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;
        }
Example #2
0
        private QLParser SetupParser(string text)
        {
            var inputStream       = new AntlrInputStream(text);
            var speakLexer        = new QLLexer(inputStream);
            var commonTokenStream = new CommonTokenStream(speakLexer);

            return(new QLParser(commonTokenStream));
        }
Example #3
0
        private QLParser CreateParser(string input)
        {
            var inputStream = new AntlrInputStream(input);
            var lexer       = new QLLexer(inputStream);
            var tokens      = new CommonTokenStream(lexer);
            var parser      = new QLParser(tokens);

            AddErrorListener(parser);
            return(parser);
        }
Example #4
0
        private Form Build(AntlrInputStream input)
        {
            var lexer       = new QLLexer(input);
            var tokenStream = new CommonTokenStream(lexer);
            var parser      = new QLParser(tokenStream);

            var visitor = new AstBuilderVisitor();
            var root    = parser.form().Accept(visitor);

            return(root as Form);
        }
Example #5
0
        internal static QLListener ParseString(string input)
        {
            ICharStream  stream = CharStreams.fromstring(input);
            ITokenSource lexer  = new QLLexer(stream);
            ITokenStream tokens = new CommonTokenStream(lexer);
            QL           parser = new QL(tokens);

            QL.FormContext  context  = parser.form();
            QLListener      listener = new QLListener();
            ParseTreeWalker walker   = new ParseTreeWalker();

            walker.Walk(listener, context);
            return(listener);
        }
Example #6
0
        public static QuestionForm ParseInputString(string input)
        {
            ICharStream  stream = CharStreams.fromstring(input);
            ITokenSource lexer  = new QLLexer(stream);
            ITokenStream tokens = new CommonTokenStream(lexer);
            QLParser     parser = new QLParser(tokens);

            QLParser.FContext context  = parser.f();
            QLListener        listener = new QLListener();
            ParseTreeWalker   walker   = new ParseTreeWalker();

            walker.Walk(listener, context);
            return(listener.Form);
        }
Example #7
0
        public QuestionForm ParseQLStream(TextReader reader)
        {
            if (reader == null) { throw new ArgumentNullException("reader"); }

            var inputStream = new AntlrInputStream(reader);

            var lexer = new QLLexer(inputStream);

            var tokens = new CommonTokenStream(lexer);

            var parser = new QLParser(tokens);

            var visitor = new QuestionFormBuilder();

            return visitor.Visit(parser.form());
        }
Example #8
0
        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();
        }
Example #9
0
        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());
        }
Example #10
0
        public static void TokenTypes(string inputString, List <string> expectdeTokenTypes)
        {
            AntlrInputStream input = new AntlrInputStream(inputString);
            QLLexer          lexer = new QLLexer(input);

            string        tokenType;
            List <string> tokenTypes = new List <string>();

            do
            {
                IToken token = lexer.NextToken();
                tokenType = lexer.Vocabulary.GetSymbolicName(token.Type);
                if (tokenType != "EOF")
                {
                    tokenTypes.Add(tokenType);
                }
            }while (tokenType != "EOF");

            CollectionAssert.AreEqual(expectdeTokenTypes, tokenTypes);
        }
Example #11
0
        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));
        }
Example #12
0
        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);
        }
Example #13
0
        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);
        }
Example #14
-1
        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));
        }