internal BnfTerms(TerminalFactoryS TerminalFactoryS) { this.OBJECT_BEGIN = TerminalFactoryS.CreateKeyTerm("{"); this.OBJECT_END = TerminalFactoryS.CreateKeyTerm("}"); this.ARRAY_BEGIN = TerminalFactoryS.CreateKeyTerm("["); this.ARRAY_END = TerminalFactoryS.CreateKeyTerm("]"); this.COMMA = TerminalFactoryS.CreateKeyTerm(","); this.COLON = TerminalFactoryS.CreateKeyTerm(":"); this.NUMBER = TerminalFactoryS.CreateNumberLiteral(); this.STRING = TerminalFactoryS.CreateStringLiteral(name: "stringliteral", startEndSymbol: "\""); this.BOOLEAN = new BnfiTermConstant <bool>() { { "true", true }, { "false", false } }; this.NULL = new BnfiTermConstantTL() { { "null", (object)null } }; }
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); }