package notpure.antlr4.macro;

import notpure.antlr4.macro.model.lang.Expression;
import notpure.antlr4.macro.model.lexer.token.Token;
import notpure.antlr4.macro.processor.lexer.SimpleLexer;
import notpure.antlr4.macro.processor.parser.SimpleParser;
import notpure.antlr4.macro.util.FileHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;

/**
 * The master processor for macro files.
 */
public final class MacroFileProcessor {

    /**
     * Logger instance.
     */
    private final static Logger LOGGER = LoggerFactory.getLogger(MacroFileProcessor.class);

    /**
     * Processes all macro files in the given directory.
     */
    public static void processDirectory(String directory) {
        // Aggregate target files
        ArrayList<String> fileNames = new ArrayList<>();
        FileHelper.getFileNames(fileNames, Paths.get(directory), ".mg4");

        // Process all files
        fileNames.forEach(MacroFileProcessor::processFile);
    }

    /**
     * Processes the given macro file.
     */
    public static void processFile(String inFileName) {
        String outFileName = FileHelper.parseFileName(inFileName) + ".g4";
        processFile(inFileName, outFileName);
    }

    /**
     * Processes the given macro file, into one with the specified name.
     */
    public static void processFile(String inFileName, String outFileName) {
        // Tokenize
        List<Token> tokens;

        try (InputStream inputStream = new FileInputStream(inFileName)) {
            tokens = new SimpleLexer().tokenize(inputStream).getTokens();
        } catch (FileNotFoundException e) {
            LOGGER.warn("File not found: '{}'", inFileName);
            return;
        } catch (IOException e) {
            LOGGER.warn("IOException occurred while processing file: '{}'", inFileName);
            return;
        }

        // Parser
        List<Expression> expressions = new SimpleParser().parse(tokens).getExpressions();

        // Resolve macro definitions
        // ...

        // Write to output file
        // ...

        // TODO finish
    }
}

@@ -71,24 +71,79 @@
 Expression;%0A
+import notpure.antlr4.macro.model.lang.ExpressionType;%0A
 import notpu
@@ -606,16 +606,52 @@
 il.List;
+%0Aimport java.util.stream.Collectors;
 %0A%0A/**%0A *
@@ -1690,24 +1690,30 @@
 ize%0A        
+final 
 List%3CToken%3E 
@@ -2174,16 +2174,22 @@
         
+final 
 List%3CExp
@@ -2300,16 +2300,472 @@
 nitions%0A
+        List%3CExpression%3E macroExpressions = expressions.stream()%0A                .filter(p -%3E p.getType() == ExpressionType.MACRO_RULE)%0A                .collect(Collectors.toList());%0A        // ...%0A%0A        // Update expressions%0A        List%3CExpression%3E ruleExpressions = expressions.stream()%0A                .filter(p -%3E p.getType() == ExpressionType.PARSER_RULE %7C%7C p.getType() == ExpressionType.LEXER_RULE)%0A                .collect(Collectors.toList());%0A
         

Update MacroFileProcessor