static bool TryExpandMacros(string input, out string output, out IEnumerable <Message> messages) { var inputStream = CharStreams.fromstring(input); var lexer = new GoToLexer(inputStream); var lexerErrorListener = new LexerErrorListener(); lexer.AddErrorListener(lexerErrorListener); var expansionMessages = new List <Message>(lexerErrorListener.Messages); var tokenStream = new CommonTokenStream(lexer); var parser = new GoToParser(tokenStream); var parserErrorListener = new ParserErrorListener(); parser.AddErrorListener(parserErrorListener); var contextSyntaxTree = parser.program(); expansionMessages.AddRange(parserErrorListener.Messages); var listener = new MacroExpansionListener(tokenStream); ParseTreeWalker.Default.Walk(listener, contextSyntaxTree); expansionMessages.AddRange(listener.Messages); messages = expansionMessages; if (AreThereErrors(expansionMessages)) { output = string.Empty; return(false); } var dirtyOutput = listener.RewrittenTokenStream.GetText(); output = dirtyOutput.TrimStart(Environment.NewLine.ToCharArray()); return(true); }
public static bool TryAnalyze( string input, out string expandedInput, out ProgramNode program, out IEnumerable <Message> messages) { program = null; var analysisMessages = new List <Message>(); messages = analysisMessages; var isExpansionSucceeded = TryExpandMacros( input, out string output, out IEnumerable <Message> expasionMessages); analysisMessages.AddRange(expasionMessages); if (isExpansionSucceeded) { expandedInput = output; } else { expandedInput = input; return(false); } var inputStream = CharStreams.fromstring(output); var lexer = new GoToLexer(inputStream); var lexerErrorListener = new LexerErrorListener(); lexer.AddErrorListener(lexerErrorListener); analysisMessages.AddRange(lexerErrorListener.Messages); var tokenStream = new CommonTokenStream(lexer); var parser = new GoToParser(tokenStream); var parserErrorListener = new ParserErrorListener(); parser.AddErrorListener(parserErrorListener); var contextSyntaxTree = parser.program(); analysisMessages.AddRange(parserErrorListener.Messages); var listener = new SemanticListener(); ParseTreeWalker.Default.Walk(listener, contextSyntaxTree); analysisMessages.AddRange(listener.Messages); if (AreThereErrors(messages)) { return(false); } var abstractSyntaxTreeGenerator = new AbstractSyntaxTreeGenerator(); program = abstractSyntaxTreeGenerator.VisitProgram(contextSyntaxTree) as ProgramNode; SemanticAnalyzer.Check(program, ref analysisMessages); if (AreThereErrors(messages)) { return(false); } return(true); }