public Questionnaire BuildAST(string inputString) { MemoryStream inputStream = new MemoryStream(Encoding.UTF8.GetBytes(inputString ?? "")); AntlrInputStream antlrInputStream = new AntlrInputStream(inputStream); QLLexer lexer = new QLLexer(antlrInputStream); CommonTokenStream tokens = new CommonTokenStream(lexer); _parser = new QLParser(tokens); //Replaxe lexer/parser error listeners lexer.RemoveErrorListeners(); _parser.RemoveErrorListeners(); lexer.AddErrorListener(new LexerErrorListener() { OnError = LexerErrors.Add }); _parser.AddErrorListener(new ParserErrorListener() { OnError = ParserErrors.Add }); //set manager on partial parser class _parser.SetIdManager(Memory); //build AST _parseTree = _parser.questionnaire(); AST = _parser.GetAST(); //check for lexer/parser errors if (!LexerErrors.Any() && !ParserErrors.Any()) { TypeChecker.Run(AST); } return AST; }
public bool Execute(DataContext context) { QLLexer lexer = new QLLexer(context.AntlrInput); lexer.AddErrorListener(new LexerErrorHandler(context.ASTHandlerExceptions)); CommonTokenStream tokens = new CommonTokenStream(lexer); QLParser parser = new QLParser(tokens); parser.AddErrorListener(new ParserErrorHandler(context.ASTHandlerExceptions)); QLListener listener = new QLListener(context.ASTHandlerExceptions); parser.AddParseListener(listener); // commence parsing the input as a formBlock since it's supposed to be the entry point of the input file parser.formBlock(); context.RootNode = listener.GetAstRootNode(); return !context.ASTHandlerExceptions.Any(); }
public bool Execute(DataContext context) { QLLexer lexer = new QLLexer(context.AntlrInput); lexer.AddErrorListener(new LexerErrorHandler(context.ASTHandlerExceptions)); CommonTokenStream tokens = new CommonTokenStream(lexer); QLParser parser = new QLParser(tokens); parser.AddErrorListener(new ParserErrorHandler(context.ASTHandlerExceptions)); QLListener listener = new QLListener(context.ASTHandlerExceptions); parser.AddParseListener(listener); // commence parsing the input as a formBlock since it's supposed to be the entry point of the input file parser.formBlock(); context.RootNode = listener.GetAstRootNode(); return(!context.ASTHandlerExceptions.Any()); }
private Reference <T> BuildAstTree <T>(string definition) where T : IAstNode { var stream = new AntlrInputStream(definition); var lexer = new QLLexer(stream); lexer.RemoveErrorListeners(); lexer.AddErrorListener(new QlErrorListener()); var tokens = new CommonTokenStream(lexer); var parser = new QLParser(tokens); parser.RemoveErrorListeners(); parser.AddErrorListener(new QlErrorListener()); var tree = parser.questionnaire(); var qlVisitor = new BuildAstVisitor(m_astFactory, m_domainItemLocator); return(qlVisitor.Visit(tree).To <T>(m_domainItemLocator)); }
public static QuestionForm ParseString(string input) { var errorHandler = new ParseErrorHandler(); var errorListener = new QLErrorListener(errorHandler); var stream = CharStreams.fromstring(input); var lexer = new QLLexer(stream); lexer.RemoveErrorListeners(); lexer.AddErrorListener(errorListener); ITokenStream tokens = new CommonTokenStream(lexer); var parser = new QL(tokens); parser.RemoveErrorListeners(); parser.AddErrorListener(errorListener); var form = parser.form().result; if (errorHandler.HasErrors) { errorHandler.ThrowParseException(); } return(form); }
public Questionnaire BuildAST(string inputString) { MemoryStream inputStream = new MemoryStream(Encoding.UTF8.GetBytes(inputString ?? "")); AntlrInputStream antlrInputStream = new AntlrInputStream(inputStream); QLLexer lexer = new QLLexer(antlrInputStream); CommonTokenStream tokens = new CommonTokenStream(lexer); _parser = new QLParser(tokens); //Replaxe lexer/parser error listeners lexer.RemoveErrorListeners(); _parser.RemoveErrorListeners(); lexer.AddErrorListener(new LexerErrorListener() { OnError = LexerErrors.Add }); _parser.AddErrorListener(new ParserErrorListener() { OnError = ParserErrors.Add }); //set manager on partial parser class _parser.SetIdManager(Memory); //build AST _parseTree = _parser.questionnaire(); AST = _parser.GetAST(); //check for lexer/parser errors if (!LexerErrors.Any() && !ParserErrors.Any()) { TypeChecker.Run(AST); } return(AST); }
private void btnParse_Click(object sender, EventArgs e) { txtOutput.Clear(); string inputString = txtInput.Text; MemoryStream inputStream = new MemoryStream(Encoding.UTF8.GetBytes(inputString ?? "")); AntlrInputStream input = new AntlrInputStream(inputStream); QLLexer lexer = new QLLexer(input); CommonTokenStream tokens = new CommonTokenStream(lexer); QLParser parser = new QLParser(tokens); lexer.RemoveErrorListeners(); parser.RemoveErrorListeners(); lexer.AddErrorListener(new LexerErrorListener(){OnError = WriteError}); parser.AddErrorListener(new ParserErrorListener(){OnError = WriteError}); IParseTree tree = parser.questionnaire(); //if (parser.NumberOfSyntaxErrors > 0) // txtOutput.Text += string.Format("Parser errors found: {0}", parser.NumberOfSyntaxErrors); QLVisitor visitor = new QLVisitor(); visitor.Visit(tree); txtOutput.Text += string.Format(@"{0}{0} Generated parse tree: {0} {1} {0} {2}" , Environment.NewLine , tree.ToStringTree(parser) , visitor.Visit(tree)); }