示例#1
0
        public JsonGrammar(Domain domain)
            : base(domain)
        {
            B = new BnfTerms(new TerminalFactoryS(this));

            this.Root = B.Object;

            B.Object.Rule =
                B.NUMBER + SetUnparsePriority((astValue, childAstValues) => astValue is int || astValue is double?(int?)1 : null)
                |
                B.STRING
                |
                B.BOOLEAN
                |
                B.Array
                |
                B.NULL + SetUnparsePriority((astValue, childAstValues) => astValue == null ? (int?)1 : null)
                |
                B.OBJECT_BEGIN
                + B.KeyValuePairs.ConvertValue(KeyValuePairsToObject, ObjectToKeyValuePairs)
                + B.OBJECT_END
            ;

            B.KeyValuePairs.Rule =
                B.KeyValuePair.PlusList(B.COMMA)
            ;

            B.KeyValuePair.Rule =
                B.Key.BindTo(B.KeyValuePair, fv => fv.Key)
                + B.COLON
                + B.Value.BindTo(B.KeyValuePair, fv => fv.Value);

            B.Key.Rule =
                B.STRING
            ;

            B.Value.Rule =
                B.Object
            ;

            B.Array.Rule =
                B.ARRAY_BEGIN
                + B.ArrayElements.BindTo(B.Array, metaArray => metaArray.Elements)
                + B.ARRAY_END
            ;

            B.ArrayElements.Rule =
                B.Object.StarListTL(B.COMMA)
            ;

            RegisterBracePair(B.OBJECT_BEGIN, B.OBJECT_END);
            RegisterBracePair(B.ARRAY_BEGIN, B.ARRAY_END);

            // TODO: although JSON does not have comments, we define C-style comments for until we solve parsing and unparsing of comments into JSON data
            CommentTerminal DelimitedComment  = new CommentTerminal("DelimitedComment", "/*", "*/");
            CommentTerminal SingleLineComment = new CommentTerminal("SingleLineComment", "//", Environment.NewLine, "\n", "\r");

            NonGrammarTerminals.Add(DelimitedComment);
            NonGrammarTerminals.Add(SingleLineComment);
        }
示例#2
0
            public Formatter(GrammarPrefix grammar)
                : base(grammar)
            {
                this.B = grammar.B;

                SyntaxHighlight = GrammarPrefix.SyntaxHighlight.Color;

                ForeColorOfOperator = Color.Red;
                ForeColorOfLiteral  = Color.ForestGreen;
            }
示例#3
0
            public Formatter(GrammarFunction grammar)
                : base(grammar)
            {
                this.B = grammar.B;

                SyntaxHighlight = GrammarFunction.SyntaxHighlight.Color;

                ForeColorOfParentheses = Color.Red;
                ForeColorOfLiteral     = Color.ForestGreen;
            }
示例#4
0
        public GrammarFunction()
            : base(new Domain())
        {
            B = new BnfTerms(new TerminalFactoryS(this));

            this.Root = B.Expression;

            B.Expression.SetRuleOr(
                B.BinaryExpression,
                B.UnaryExpression,
                B.ConditionalTernaryExpression,
                B.NumberLiteral,
                B.BoolLiteral
                );

            B.BinaryExpression.Rule =
                B.BinaryOperator.BindTo(B.BinaryExpression, t => t.Op)
                + B.LEFT_PAREN
                + B.Expression.BindTo(B.BinaryExpression, t => t.Term1)
                + B.COMMA
                + B.Expression.BindTo(B.BinaryExpression, t => t.Term2)
                + B.RIGHT_PAREN
            ;

            B.UnaryExpression.Rule =
                B.UnaryOperator.BindTo(B.UnaryExpression, t => t.Op)
                + B.LEFT_PAREN
                + B.Expression.BindTo(B.UnaryExpression, t => t.Term)
                + B.RIGHT_PAREN
            ;

            B.ConditionalTernaryExpression.Rule =
                B.CONDITIONAL_TERNARY
                + B.LEFT_PAREN
                + B.Expression.BindTo(B.ConditionalTernaryExpression, t => t.Cond)
                + B.COMMA
                + B.Expression.BindTo(B.ConditionalTernaryExpression, t => t.Term1)
                + B.COMMA
                + B.Expression.BindTo(B.ConditionalTernaryExpression, t => t.Term2)
                + B.RIGHT_PAREN
            ;

            var numberLiteralInfo = new NumberLiteralInfo()
                                    .AddPrefix("#b", NumberLiteralBase.Binary)
                                    .AddPrefix("#o", NumberLiteralBase.Octal)
                                    .AddPrefix("#x", NumberLiteralBase.Hexadecimal);

            B.NumberLiteral.Rule = TerminalFactoryS.CreateNumberLiteral <D.NumberLiteral>(numberLiteralInfo);
            B.BoolLiteral.Rule   = B.BOOL_CONSTANT.BindTo(B.BoolLiteral, t => t.Value);

            B.BinaryOperator.Rule = B.ADD_OP | B.SUB_OP | B.MUL_OP | B.DIV_OP | B.POW_OP | B.MOD_OP | B.EQ_OP | B.NEQ_OP | B.LT_OP | B.LTE_OP | B.GT_OP | B.GTE_OP | B.AND_OP | B.OR_OP;
            B.UnaryOperator.Rule  = B.POS_OP | B.NEG_OP | B.NOT_OP;

            RegisterBracePair(B.LEFT_PAREN, B.RIGHT_PAREN);
        }
示例#5
0
            public Formatter(JsonGrammar grammar)
                : base(grammar)
            {
                this.B = grammar.B;

                this.CompactFormat   = true;
                this.ShowTypesInBold = true;

                ForeColorOfKeyword = Color.Red;
                ForeColorOfKey     = Color.Blue;
                ForeColorOfType    = Color.ForestGreen;
                ForeColorOfValue   = Color.Black;
                ForeColorOfLiteral = Color.Violet;
                ForeColorOfComment = Color.DarkGray;

                MultiLineCommentDecorator = " *";
            }
示例#6
0
            public Formatter(GrammarP grammar)
                : base(grammar)
            {
                this.B = grammar.B;

                SyntaxHighlight    = GrammarP.SyntaxHighlight.Color;
                FlattenIfHierarchy = true;

                ForeColorOfKeyword  = Color.Blue;
                ForeColorOfOperator = Color.Red;
                ForeColorOfType     = Color.Cyan;
                ForeColorOfLiteral  = Color.Purple;
                ForeColorOfComment  = Color.DarkGreen;

                SpecialSyntaxHighlightForColorLiterals = true;

                MultiLineCommentDecorator = " @";
            }
示例#7
0
            public Formatter(GrammarC grammar)
                : base(grammar)
            {
                this.B = grammar.B;

                SyntaxHighlight    = GrammarC.SyntaxHighlight.Color;
                FlattenIfHierarchy = true;
                IndentStyle        = GrammarC.IndentStyle.Allman;

                ForeColorOfKeyword  = Color.Blue;
                ForeColorOfOperator = Color.Red;
                ForeColorOfType     = Color.Cyan;
                ForeColorOfLiteral  = Color.Purple;
                ForeColorOfComment  = Color.DarkGreen;

                ShowNamesInBold      = true;
                ShowNameRefsInItalic = false;

                SpecialSyntaxHighlightForColorLiterals = true;

                MultiLineCommentDecorator = " *";
            }
示例#8
0
        public GrammarP(CultureInfo cultureInfo)
            : base(new Domain())
        {
            B = new BnfTerms(new TerminalFactoryS(this), cultureInfo);

            this.DefaultCulture = cultureInfo;

            this.Root = B.Program;

            B.Program.Rule =
                B.PROGRAM
                + B.Name.BindTo(B.Program, t => t.Name)
                + (B.NAMESPACE + B.NamespaceName).QRef().BindTo(B.Program, t => t.Namespace)
                + B.Function.StarList().BindTo(B.Program, t => t.Functions)
                + B.BEGIN
                + B.Statement.PlusList().BindTo(B.Program, t => t.Body)
                + B.END
                + B.DOT
            ;

            B.Function.Rule =
                B.FUNCTION
                + B.ASYNC.QVal(false).BindTo(B.Function, t => t.IsAsync)
                + B.Name.BindTo(B.Function, t => t.Name)
                + B.LEFT_PAREN
                + B.Parameter.StarList(B.COMMA).BindTo(B.Function, t => t.Parameters)
                + B.RIGHT_PAREN
                + (B.COLON + B.Type).QVal().BindTo(B.Function, t => t.ReturnType)
                + B.BEGIN
                + B.Statement.PlusList().BindTo(B.Function, t => t.Body)
                + B.END
            ;

            B.Parameter.Rule =
                B.VAR
                + B.Name.BindTo(B.Parameter, t => t.Name)
                + B.COLON
                + B.Type.BindTo(B.Parameter, t => t.Type)
            ;

            B.Statement.SetRuleOr(
                B.LocalVariable + B.SEMICOLON,
                B.Assignment + B.SEMICOLON,
                B.While,
                B.For,
                B.If,
#if SEPARATE_IFELSE
                B.IfElse,
#endif
                B.FunctionCall + B.SEMICOLON,
                B.Write + B.SEMICOLON,
                B.WriteLn + B.SEMICOLON,
                B.Return + B.SEMICOLON,
                B.StatementList
                );

            B.Return.Rule =
                B.RETURN
                + B.Expression.BindTo(B.Return, t => t.Value)
            ;

            B.LocalVariable.Rule =
                B.VAR
                + B.Name.BindTo(B.LocalVariable, t => t.Name)
                + B.COLON
                + B.Type.BindTo(B.LocalVariable, t => t.Type)
                + (B.LET + B.Expression).QRef().BindTo(B.LocalVariable, t => t.InitValue)
            ;

            B.Assignment.Rule =
                B.VariableReference.BindTo(B.Assignment, t => t.LValue)
                + B.LET
                + B.Expression.BindTo(B.Assignment, t => t.RValue)
            ;

            B.VariableReference.Rule =
                B.NameRef
                .ConvertValue(_nameRef => ReferenceFactory.Get <D.IVariable>(_nameRef), _variableReference => _variableReference.NameRef)
                .BindTo(B.VariableReference, t => t.Target)
            ;

            B.FunctionReference.Rule =
                B.NameRef.ConvertValue(_nameRef => ReferenceFactory.Get <D.Function>(_nameRef), _functionReference => _functionReference.NameRef)
            ;

            B.StatementList.Rule =
                B.BEGIN
                + B.Statement.PlusList().BindTo(B.StatementList, t => t.Body)
                + B.END
            ;

            B.While.Rule =
                B.WHILE
                + B.LEFT_PAREN
                + B.Expression.BindTo(B.While, t => t.Condition)
                + B.RIGHT_PAREN
                + B.DO
                + B.Statement.BindTo(B.While, t => t.Body)
            ;

            B.For.Rule =
                B.FOR
                + B.LEFT_PAREN
                + B.LocalVariable.StarList(B.COMMA).BindTo(B.For, t => t.Init)
                + B.SEMICOLON
                + B.Expression.BindTo(B.For, t => t.Condition)
                + B.SEMICOLON
                + B.Assignment.StarList(B.COMMA).BindTo(B.For, t => t.Update)
                + B.RIGHT_PAREN
                + B.DO
                + B.Statement.BindTo(B.For, t => t.Body)
            ;

#if SEPARATE_IFELSE
            B.If.Rule =
                B.IF
                + B.LEFT_PAREN
                + B.Expression.BindTo(B.If, t => t.Condition)
                + B.RIGHT_PAREN
                + B.THEN
                + B.Statement.BindTo(B.If, t => t.Body)
            ;

            B.IfElse.Rule =
                B.If.Copy(B.IfElse)
                + B.ELSE
                + B.Statement.BindTo(B.IfElse, t => t.ElseBody)
            ;
#else
            B.If.Rule =
                B.IF
                + B.LEFT_PAREN
                + B.Expression.BindTo(B.If, t => t.Condition)
                + B.RIGHT_PAREN
                + B.THEN
                + B.Statement.BindTo(B.If, t => t.Body)
                + (B.ELSE + B.Statement).QRef().BindTo(B.If, t => t.ElseBody)
            ;
#endif

            B.FunctionCall.Rule =
                B.FunctionReference.BindTo(B.FunctionCall, t => t.FunctionReference)
                + B.LEFT_PAREN
                + B.Argument.StarList(B.COMMA).BindTo(B.FunctionCall, t => t.Arguments)
                + B.RIGHT_PAREN
            ;

            B.Argument.Rule =
                B.Expression.BindTo(B.Argument, t => t.Expression)
            ;

            B.Write.Rule =
                B.WRITE
                + B.LEFT_PAREN
                + B.Expression.StarList(B.COMMA).BindTo(B.Write, t => t.Arguments)
                + B.RIGHT_PAREN
            ;

            B.WriteLn.Rule =
                B.WRITELN
                + B.LEFT_PAREN
                + B.Expression.StarList(B.COMMA).BindTo(B.WriteLn, t => t.Arguments)
                + B.RIGHT_PAREN
            ;

            B.Name.Rule          = B.IDENTIFIER.BindTo(B.Name, t => t.Value);
            B.NameRef.Rule       = B.IDENTIFIER.ConvertValue(_identifier => new NameRef(_identifier), _nameRef => _nameRef.Value);
            B.NamespaceName.Rule =
                B.IDENTIFIER
                .PlusList(B.DOT)
                .ConvertValue(
                    _identifiers => new NameRef(string.Join(B.DOT.Text, _identifiers)),
                    _nameRef => _nameRef.Value.Split(new string[] { B.DOT.Text }, StringSplitOptions.None)
                    );

            B.Expression.SetRuleOr(
                B.BinaryExpression,
                B.UnaryExpression,
                B.ConditionalTernaryExpression,
                B.NumberLiteral,
                B.DateLiteral,
                B.StringLiteral,
                B.BoolLiteral,
                B.ColorLiteral,
                B.FunctionCall,
                B.VariableReference,
                B.LEFT_PAREN + B.Expression + B.RIGHT_PAREN
                );

            B.BinaryExpression.Rule =
                B.Expression.BindTo(B.BinaryExpression, t => t.Term1)
                + B.BinaryOperator.BindTo(B.BinaryExpression, t => t.Op)
                + B.Expression.BindTo(B.BinaryExpression, t => t.Term2)
            ;

            /*
             * NOTE: ImplyPrecedenceHere does not work properly, so we do not use it (it parsed operator NEG as operator POS, and omitted the expression after).
             * So we use ReduceHere instead, which means that unary operators has the highest precedence among operators when used inside a unary expressions.
             * */
            B.UnaryExpression.Rule =
                B.UnaryOperator.BindTo(B.UnaryExpression, t => t.Op)
                + B.Expression.BindTo(B.UnaryExpression, t => t.Term)
                + ReduceHere()      // this is needed for implying precedence (see note above)
            ;

            B.ConditionalTernaryExpression.Rule =
                B.Expression.BindTo(B.ConditionalTernaryExpression, t => t.Cond)
                + B.QUESTION_MARK
                + B.Expression.BindTo(B.ConditionalTernaryExpression, t => t.Term1)
                + B.COLON
                + B.Expression.BindTo(B.ConditionalTernaryExpression, t => t.Term2)
            ;

            var numberLiteralInfo = new NumberLiteralInfo()
                                    .AddSuffix("D", TypeCode.Double)
                                    .AddSuffix("M", TypeCode.Decimal)
                                    .AddPrefix("#b", NumberLiteralBase.Binary)
                                    .AddPrefix("#o", NumberLiteralBase.Octal)
                                    .AddPrefix("#x", NumberLiteralBase.Hexadecimal);

            B.NumberLiteral.Rule = TerminalFactoryS.CreateNumberLiteral <DE.NumberLiteral>(numberLiteralInfo);
//            B.NumberLiteral.Rule = TerminalFactoryS.CreateNumberLiteral().BindTo(B.NumberLiteral, t => t.Value);   // B.NumberLiteral used to be a BnfiTermRecord
            B.StringLiteral.Rule = TerminalFactoryS.CreateStringLiteral(name: "stringliteral", startEndSymbol: "'").BindTo(B.StringLiteral, t => t.Value);
            B.BoolLiteral.Rule   = B.BOOL_CONSTANT.BindTo(B.BoolLiteral, t => t.Value);
            B.ColorLiteral.Rule  = B.COLOR_CONSTANT.BindTo(B.ColorLiteral, t => t.Value);
            B.DateLiteral.Rule   = TerminalFactoryS.CreateDataLiteralDateTimeQuoted(name: "dateliteral", startEndSymbol: "$", dateTimeFormat: D.DateLiteral.Format).BindTo(B.DateLiteral, t => t.Value);

            B.BinaryOperator.Rule = B.ADD_OP | B.SUB_OP | B.MUL_OP | B.DIV_OP | B.POW_OP | B.MOD_OP | B.EQ_OP | B.NEQ_OP | B.LT_OP | B.LTE_OP | B.GT_OP | B.GTE_OP | B.AND_OP | B.OR_OP;
            B.UnaryOperator.Rule  = B.POS_OP | B.NEG_OP | B.NOT_OP;

            B.Type.Rule = B.INTEGER_TYPE | B.REAL_TYPE | B.STRING_TYPE | B.CHAR_TYPE | B.BOOL_TYPE | B.COLOR_TYPE | B.DATE_TYPE;

            /*
             * NOTE: RegisterOperators in Irony is string-based, therefore it is impossible to specify different precedences
             * for binary '+' and unary '+', and for binary '-' and unary '-', so we encode the precedences of unary operators
             * into the grammar by specifying a ReduceHere() hint after unary expressions.
             * */
            RegisterOperators(10, Associativity.Right, B.QUESTION_MARK, B.COLON);
            RegisterOperators(20, B.OR_OP);
            RegisterOperators(30, B.AND_OP);
            RegisterOperators(40, B.EQ_OP, B.NEQ_OP);
            RegisterOperators(50, B.LT_OP, B.LTE_OP, B.GT_OP, B.GTE_OP);
            RegisterOperators(60, B.ADD_OP, B.SUB_OP);
            RegisterOperators(70, B.MUL_OP, B.DIV_OP, B.MOD_OP);
            RegisterOperators(80, Associativity.Right, B.POW_OP);
            RegisterOperators(90, Associativity.Neutral, recurse: false, operators: new[] { B.NEG_OP, B.POS_OP, B.NOT_OP });
            // NOTE: for the parser the unary operators precedences are encoded into the grammar, but for the unparser we have to specify the precedences
            // NOTE: we must not recurse, since NEG_OP and POS_OP has the same terminals as SUB_OP and ADD_OP, respectively ('-' and '+').

            RegisterBracePair(B.LEFT_PAREN, B.RIGHT_PAREN);

            CommentTerminal DelimitedComment  = new CommentTerminal("DelimitedComment", "(@", "@)");
            CommentTerminal SingleLineComment = new CommentTerminal("SingleLineComment", "@@", Environment.NewLine, "\n", "\r");

            NonGrammarTerminals.Add(DelimitedComment);
            NonGrammarTerminals.Add(SingleLineComment);
        }
示例#9
0
        public GrammarInfix()
            : base(new Domain())
        {
            B = new BnfTerms(new TerminalFactoryS(this));

            this.Root = B.Expression;

            B.Expression.SetRuleOr(
                B.BinaryExpression,
                B.UnaryExpression,
                B.ConditionalTernaryExpression,
                B.NumberLiteral,
                B.BoolLiteral,
                B.LEFT_PAREN + B.Expression + B.RIGHT_PAREN
                );

            B.BinaryExpression.Rule =
                B.Expression.BindTo(B.BinaryExpression, t => t.Term1)
                + B.BinaryOperator.BindTo(B.BinaryExpression, t => t.Op)
                + B.Expression.BindTo(B.BinaryExpression, t => t.Term2)
            ;

            /*
             * NOTE: ImplyPrecedenceHere does not work properly, so we do not use it (it parsed operator NEG as operator POS, and omitted the expression after).
             * So we use ReduceHere instead, which means that unary operators has the highest precedence among operators when used inside a unary expressions.
             * */
            B.UnaryExpression.Rule =
                B.UnaryOperator.BindTo(B.UnaryExpression, t => t.Op)
                + B.Expression.BindTo(B.UnaryExpression, t => t.Term)
                + ReduceHere()      // this is needed for implying precedence (see note above)
            ;

            B.ConditionalTernaryExpression.Rule =
                B.Expression.BindTo(B.ConditionalTernaryExpression, t => t.Cond)
                + B.QUESTION_MARK
                + B.Expression.BindTo(B.ConditionalTernaryExpression, t => t.Term1)
                + B.COLON
                + B.Expression.BindTo(B.ConditionalTernaryExpression, t => t.Term2)
            ;

            var numberLiteralInfo = new NumberLiteralInfo()
                                    .AddPrefix("#b", NumberLiteralBase.Binary)
                                    .AddPrefix("#o", NumberLiteralBase.Octal)
                                    .AddPrefix("#x", NumberLiteralBase.Hexadecimal);

            B.NumberLiteral.Rule = TerminalFactoryS.CreateNumberLiteral <D.NumberLiteral>(numberLiteralInfo);
            B.BoolLiteral.Rule   = B.BOOL_CONSTANT.BindTo(B.BoolLiteral, t => t.Value);

            B.BinaryOperator.Rule = B.ADD_OP | B.SUB_OP | B.MUL_OP | B.DIV_OP | B.POW_OP | B.MOD_OP | B.EQ_OP | B.NEQ_OP | B.LT_OP | B.LTE_OP | B.GT_OP | B.GTE_OP | B.AND_OP | B.OR_OP;
            B.UnaryOperator.Rule  = B.POS_OP | B.NEG_OP | B.NOT_OP;

            /*
             * NOTE: RegisterOperators in Irony is string-based, therefore it is impossible to specify different precedences
             * for binary '+' and unary '+', and for binary '-' and unary '-', so we encode the precedences of unary operators
             * into the grammar by specifying a ReduceHere() hint after unary expressions.
             * */
            RegisterOperators(10, Associativity.Right, B.QUESTION_MARK, B.COLON);
            RegisterOperators(20, B.OR_OP);
            RegisterOperators(30, B.AND_OP);
            RegisterOperators(40, B.EQ_OP, B.NEQ_OP);
            RegisterOperators(50, B.LT_OP, B.LTE_OP, B.GT_OP, B.GTE_OP);
            RegisterOperators(60, B.ADD_OP, B.SUB_OP);
            RegisterOperators(70, B.MUL_OP, B.DIV_OP, B.MOD_OP);
            RegisterOperators(80, Associativity.Right, B.POW_OP);
            RegisterOperators(90, Associativity.Neutral, recurse: false, operators: new[] { B.NEG_OP, B.POS_OP, B.NOT_OP });
            // NOTE: for the parser the unary operators precedences are encoded into the grammar, but for the unparser we have to specify the precedences
            // NOTE: we must not recurse, since NEG_OP and POS_OP has the same terminals as SUB_OP and ADD_OP, respectively ('-' and '+').

            RegisterBracePair(B.LEFT_PAREN, B.RIGHT_PAREN);
        }