Example #1
0
 protected void Build(string input)
 {
     Inputstream = new AntlrInputStream(input);
     Lexer = new QLLexer(Inputstream);
     Tokenstream = new CommonTokenStream(Lexer);
     Parser = new QLParser(Tokenstream);
 }
Example #2
0
        public override void ExitControlUnit(QLParser.ControlUnitContext context)
        {
            IList<ElementBase> children = GetChildren();

            ControlUnit controlUnit;
            if (children.Count() == 3)
            {
                controlUnit = new ControlUnit(
                    (Expression)children[0],
                    (Block)children[1],
                    (Block)children[2],
                    SourceLocation.CreateFor(context)
                    );
            }
            else if (children.Count() == 2)
            {
                controlUnit = new ControlUnit(
                    (Expression)children[0],
                    (Block)children[1],
                    SourceLocation.CreateFor(context)
                    );
            }
            else
            {
                _astBuilderExceptions.Add(new ParserError("Bad number of controlUnit children:" + children.Count()));
                return;
            }

            AppendToAST(controlUnit);
        }
 protected void Build(string input)
 {
     Inputstream = new AntlrInputStream(input);
     Lexer       = new QLLexer(Inputstream);
     Tokenstream = new CommonTokenStream(Lexer);
     Parser      = new QLParser(Tokenstream);
 }
Example #4
0
        public override void ExitBlock(QLParser.BlockContext context)
        {
            IList<ElementBase> children = GetChildren();

            Block block = new Block(children, SourceLocation.CreateFor(context));
            AppendToAST(block);
        }
Example #5
0
        public void TestWarnings()
        {
            var presenter = new MainPresenter(Form);
            var duplicateLabelFormLocation = _pathToValidForms + "DuplicateLabel.txt";
            var formContent = File.ReadAllText(duplicateLabelFormLocation);
            var astForm     = QLParser.ParseString(formContent);
            var messages    = MainPresenter.ValidateForm(astForm);

            Assert.IsTrue(messages.Warnings.Any());
        }
Example #6
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 #7
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 #8
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 #9
0
        public void TestValidForms()
        {
            var presenter  = new MainPresenter(Form);
            var validForms = Directory.GetFiles(_pathToValidForms);

            foreach (var validForm in validForms)
            {
                // Call checker and assert no errors
                var validFormContent = File.ReadAllText(validForm);
                var astForm          = QLParser.ParseString(validFormContent);
                var messages         = MainPresenter.ValidateForm(astForm);
                Assert.IsTrue(!messages.Errors.Any());
            }
        }
Example #10
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 #11
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 #12
0
        public ConsoleTypeChecker()
            : base()
        {
            parser = new QLParser<IExprNode, IStmntNode>();
            parser.Factory = factory = new QLFactory();

            parser.OnReduction += OnReduction;
            parser.OnCompletion += OnCompletion;
            parser.OnGroupError += OnGroupError;
            parser.OnInternalError += OnInternalError;
            parser.OnNotLoadedError += OnNotLoadedError;
            parser.OnLexicalError += OnLexicalError;
            parser.OnSyntaxError += OnSyntaxError;

            Assembly a = typeof(QLParser<IExprNode, IStmntNode>).Assembly;
            parser.LoadGrammar(new BinaryReader(a.GetManifestResourceStream("QL_Grammar.Grammar.QL_Grammar.egt")));
            parser.Parse(System.IO.File.OpenText(@"..\..\..\..\..\Grammar\QL_Test.txt"));
        }
Example #13
0
        public void TestNonParsableorms()
        {
            var nonParsableForms = Directory.GetFiles(_pathToNonParsableForms);

            foreach (var nonParsableForm in nonParsableForms)
            {
                // Call parser and assert that exception is thrown
                Exception expectedException      = null;
                var       nonParsableFormContent = File.ReadAllText(nonParsableForm);
                try
                {
                    QLParser.ParseString(nonParsableFormContent);
                }
                catch (QLParseException exception)
                {
                    expectedException = exception;
                }
                Assert.IsNotNull(expectedException);
            }
        }
Example #14
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 #15
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 #16
0
        public ParsingTask Process(ParsingTask input)
        {
            if (string.IsNullOrEmpty(input.ParsingInput))
            {
                input.Errors.Add("Input string is empty");
                _canContinue = false;
                return(input);
            }

            QLParser parser        = SetupParser(input.ParsingInput);
            var      errorListener = new ErrorListener();

            parser.AddErrorListener(errorListener);

            var visitor = new ParseTreeVisitor(new OperatorFactory(new ValueFactory()));

            input.Ast = visitor.Visit(parser.form());
            errorListener.Errors.ForEach(x => input.Errors.Add(x));
            _canContinue = !errorListener.Errors.Any();

            return(input);
        }
Example #17
0
        private void ParseFile(string inputFile)
        {
            var fileContent = File.ReadAllText(inputFile);

            try
            {
                var astForm  = QLParser.ParseString(fileContent);
                var messages = ValidateForm(astForm);
                if (AnyErrors(messages))
                {
                    _view.SetErrors(messages.Errors);
                    return;
                }
                _executor = new QLExecutor(astForm);

                var qlsFileLocation            = inputFile + ".qls";
                IQuestionFormRenderer renderer = new QLRenderer(_executor);
                if (File.Exists(qlsFileLocation))
                {
                    var styleSheet = QLSParser.ParseString(File.ReadAllText(qlsFileLocation));
                    messages.Add(ValidateStyleSheet(styleSheet, astForm));
                    if (AnyErrors(messages))
                    {
                        _view.SetErrors(messages.Errors);
                        return;
                    }
                    renderer = new QLSRenderer(_executor, styleSheet);
                }
                _view.SetFormControl(renderer.Render());
                _view.SetWarnings(messages.Warnings);
            }
            catch (QLParseException exception)
            {
                _view.SetErrors(exception.Exceptions);
            }
        }
 private IStaticReturnType GetTypeInstanceFor(QLParser.YesnoTypeContext typeContext)
 {
     return new Yesno();
 }
 public IStaticReturnType GetTypeInstance(QLParser.TypeContext typeContext)
 {
     return GetTypeInstanceFor((dynamic)typeContext);
 }
Example #20
0
 private void AddErrorListener(QLParser parser)
 {
     parser.RemoveErrorListeners();
     parser.AddErrorListener(new AntlrErrorListener(Messages));
 }
Example #21
0
 public override void ExitNumber(QLParser.NumberContext context)
 {
     Number literal = new Number(context.NUMBER().GetText(), SourceLocation.CreateFor(context));
     AppendToAST(literal);
 }
Example #22
0
 public override void ExitOperatorGreaterThan(QLParser.OperatorGreaterThanContext context)
 {
     BinaryTreeElementBase op = new GreaterThanOperator(SourceLocation.CreateFor(context));
     AppendToAST(op);
 }
Example #23
0
        public override void ExitStatementUnit(QLParser.StatementUnitContext context)
        {
            IList<ElementBase> children = GetChildren();

            ThrowExceptionIfAny();
            if (children.Count() != 2)
            {
                _astBuilderExceptions.Add(new ParserError("A statement should have only expression and an identifier as children."));
            }

            StatementUnit statement = new StatementUnit(
                (Identifier)children[0],
                (Expression)children[1],
                context.TEXT().GetText(),
                _terminalTypeFactory.GetTypeInstance(context.type()),
                SourceLocation.CreateFor(context)
                );
            AppendToAST(statement);
        }
 private IStaticReturnType GetTypeInstanceFor(QLParser.TypeContext typeContext)
 {
     throw new ParserError("Unrecognised type: " + typeContext);
 }
Example #25
0
 public override void EnterFormBlock(QLParser.FormBlockContext context)
 {
     InitializeNewLevel();
 }
Example #26
0
 public override void ExitOperatorSubtraction(QLParser.OperatorSubtractionContext context)
 {
     BinaryTreeElementBase op = new MinusOperator(SourceLocation.CreateFor(context));
     AppendToAST(op);
 }
Example #27
0
        public override void ExitQuestionUnit(QLParser.QuestionUnitContext context)
        {
            IList<ElementBase> children = GetChildren();

            ThrowExceptionIfAny();
            if (children.Count() != 1)
            {
                _astBuilderExceptions.Add(new ParserError("A question should have only identifier as a child."));
            }

            QuestionUnit question = new QuestionUnit(
                (Identifier)children[0],
                _terminalTypeFactory.GetTypeInstance(context.type()),
                context.TEXT().GetText(),
                SourceLocation.CreateFor(context)
                );
            AppendToAST(question);
        }
Example #28
0
        public override void ExitExpression(QLParser.ExpressionContext context)
        {
            IList<ElementBase> children = GetChildren();
            Expression expression;

            if (children.Count() == 1)
            {
                expression = new Expression(children[0], SourceLocation.CreateFor(context));
            }
            else if (children.Count() == 3)
            {
                ElementBase leftNode = children[0];
                BinaryTreeElementBase operatorNode = (BinaryTreeElementBase)children[1];
                ElementBase rightNode = children[2];

                operatorNode.Left = leftNode;
                operatorNode.Right = rightNode;

                expression = new Expression(operatorNode, SourceLocation.CreateFor(context));
            }
            else
            {
                _astBuilderExceptions.Add(new ParserError("Expression without a child"));
                return;
            }

            AppendToAST(expression);
        }
Example #29
0
 public override void EnterUnit(QLParser.UnitContext context)
 {
     InitializeNewLevel();
 }
 private IStaticReturnType GetTypeInstanceFor(QLParser.NumberTypeContext typeContext)
 {
     return new Number();
 }
Example #31
0
        public override void ExitFormBlock(QLParser.FormBlockContext context)
        {
            IList<ElementBase> children = GetChildren();

            if (children.Count() != 2)
            {
                _astBuilderExceptions.Add(new ParserError("initial form block should have two children", SourceLocation.CreateFor(context)));
            }

            Form form = new Form(
                (Identifier)children[0],
                (Block)children[1],
                SourceLocation.CreateFor(context)
                );

            AppendToAST(form);
        }
 private IStaticReturnType GetTypeInstanceFor(QLParser.TextTypeContext typeContext)
 {
     return new Text();
 }
Example #33
0
 public override void EnterExpression(QLParser.ExpressionContext context)
 {
     InitializeNewLevel();
 }
Example #34
0
 public override void ExitOperatorNotEquals(QLParser.OperatorNotEqualsContext context)
 {
     BinaryTreeElementBase op = new NotEqualsOperator(SourceLocation.CreateFor(context));
     AppendToAST(op);
 }
Example #35
0
 public override void ExitText(QLParser.TextContext context)
 {
     Text literal = new Text(context.TEXT().GetText(), SourceLocation.CreateFor(context));
     AppendToAST(literal);
 }
Example #36
0
 public override void ExitIdentifier(QLParser.IdentifierContext context)
 {
     Identifier literal = new Identifier(context.IDENTIFIER().GetText(), SourceLocation.CreateFor(context));
     AppendToAST(literal);
 }
Example #37
0
 public override void ExitOperatorLessThanOrEqualTo(QLParser.OperatorLessThanOrEqualToContext context)
 {
     BinaryTreeElementBase op = new LessThanEqualToOperator(SourceLocation.CreateFor(context));
     AppendToAST(op);
 }
Example #38
0
 private QLParser<IExprNode, IStmntNode> InitParser()
 {
     QLParser<IExprNode, IStmntNode> parser = new QLParser<IExprNode, IStmntNode>();
     parser.Factory = new QLFactory();
     Assembly a = typeof(QLParser<IExprNode, IStmntNode>).Assembly;
     parser.LoadGrammar(new BinaryReader(a.GetManifestResourceStream("QL_Grammar.Grammar.QL_Grammar.egt")));
     return parser;
 }
Example #39
0
 public override void ExitYesno(QLParser.YesnoContext context)
 {
     Yesno literal = new Yesno(context.YESNO().GetText(), SourceLocation.CreateFor(context));
     AppendToAST(literal);
 }