/*
 * Copyright (c) 2015-2019 Dzikoysk
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.panda_lang.panda.framework.language.resource.parsers.scope.block.looping;

import org.panda_lang.panda.framework.language.interpreter.parser.PandaPipelines;
import org.panda_lang.panda.framework.design.interpreter.parser.ParserData;
import org.panda_lang.panda.framework.design.interpreter.parser.bootstrap.BootstrapParserBuilder;
import org.panda_lang.panda.framework.design.interpreter.parser.bootstrap.annotations.Autowired;
import org.panda_lang.panda.framework.design.interpreter.parser.bootstrap.annotations.Src;
import org.panda_lang.panda.framework.design.interpreter.parser.bootstrap.handlers.TokenHandler;
import org.panda_lang.panda.framework.design.interpreter.parser.bootstrap.interceptor.AbyssPatternInterceptor;
import org.panda_lang.panda.framework.design.interpreter.parser.component.UniversalComponents;
import org.panda_lang.panda.framework.design.resource.parsers.ParserRegistration;
import org.panda_lang.panda.framework.design.interpreter.pattern.utils.AbyssPatternData;
import org.panda_lang.panda.framework.design.interpreter.token.snippet.Snippet;
import org.panda_lang.panda.framework.design.runtime.expression.Expression;
import org.panda_lang.panda.framework.language.architecture.dynamic.block.looping.WhileBlock;
import org.panda_lang.panda.framework.language.interpreter.parser.PandaParserException;
import org.panda_lang.panda.framework.language.resource.parsers.scope.block.BlockData;
import org.panda_lang.panda.framework.language.resource.parsers.scope.block.BlockSubparserBootstrap;
import org.panda_lang.panda.framework.language.resource.syntax.keyword.Keywords;

@ParserRegistration(target = PandaPipelines.BLOCK_LABEL)
public class WhileParser extends BlockSubparserBootstrap {

    @Override
    protected BootstrapParserBuilder<BlockData> initialize(ParserData data, BootstrapParserBuilder<BlockData> defaultBuilder) {
        return defaultBuilder
                .handler(new TokenHandler(Keywords.WHILE))
                .interceptor(new AbyssPatternInterceptor())
                .pattern(new AbyssPatternData("while ( +* )", "while-expression"));
    }

    @Autowired
    private BlockData parseWhile(ParserData data, @Src("while-expression") Snippet expressionSource) {
        Expression expression = data.getComponent(UniversalComponents.EXPRESSION).parse(data, expressionSource);

        if (!expression.getReturnType().isClassOf("Boolean")) {
            throw new PandaParserException("Loop requires boolean as an argument");
        }

        return new BlockData(new WhileBlock(expression));
    }

}

@@ -686,90 +686,8 @@
 g;%0A%0A
-import org.panda_lang.panda.framework.language.interpreter.parser.PandaPipelines;%0A
 impo
@@ -1145,119 +1145,8 @@
 er;%0A
-import org.panda_lang.panda.framework.design.interpreter.parser.bootstrap.interceptor.AbyssPatternInterceptor;%0A
 impo
@@ -1236,16 +1236,96 @@
 onents;%0A
+import org.panda_lang.panda.framework.design.interpreter.token.snippet.Snippet;%0A
 import o
@@ -1402,177 +1402,8 @@
 on;%0A
-import org.panda_lang.panda.framework.design.interpreter.pattern.utils.AbyssPatternData;%0Aimport org.panda_lang.panda.framework.design.interpreter.token.snippet.Snippet;%0A
 impo
@@ -1652,24 +1652,106 @@
 rException;%0A
+import org.panda_lang.panda.framework.language.interpreter.parser.PandaPipelines;%0A
 import org.p
@@ -2377,104 +2377,23 @@
    .
-interceptor(new AbyssPatternInterceptor())%0A                .pattern(new AbyssPatternData
+pattern
 (%22while 
 ( +*
@@ -2392,36 +2392,25 @@
 ile 
+%60
 ( 
-+* )%22, %22while-expression%22)
+%3C*content%3E %60)%22
 );%0A 
@@ -2490,24 +2490,16 @@
 rc(%22
-while-expression
+*content
 %22) S
@@ -2505,26 +2505,23 @@
 Snippet 
-expression
+content
 Source) 
@@ -2616,26 +2616,23 @@
 e(data, 
-expression
+content
 Source);

Replace AbyssPattern with TokenPattern in the While parser