Inheritance: CompoundTerminalBase
 //http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-334.pdf section 9.4.4
 public static NumberLiteral CreateCSharpNumber(string name)
 {
     NumberLiteral term = new NumberLiteral(name, TermOptions.EnableQuickParse | TermOptions.SpecialIgnoreCase);
       term.DefaultIntTypes = new TypeCode[] { TypeCode.Int32, TypeCode.UInt32, TypeCode.Int64, TypeCode.UInt64 };
       term.DefaultFloatType = TypeCode.Double;
       term.AddPrefixFlag("0x", ScanFlags.Hex);
       term.AddSuffixCodes("u", TypeCode.UInt32, TypeCode.UInt64);
       term.AddSuffixCodes("l", TypeCode.Int64, TypeCode.UInt64);
       term.AddSuffixCodes("ul", TypeCode.UInt64);
       term.AddSuffixCodes("f", TypeCode.Single);
     term.AddSuffixCodes("d", TypeCode.Double);
       term.AddSuffixCodes("m", TypeCode.Decimal);
       return term;
 }
示例#2
0
        public BasicGrammar()
        {
            #region Initialisation

            // BASIC is not case sensitive...
            this.CaseSensitive = false;

            // By default, new-line characters are ignored.  Because BASIC uses
            // line breaks to delimit lines, we need to know where the line breaks
            // are.  The following line is required for this.
            this.TokenFilters.Add(new CodeOutlineFilter(false));

            // Define the Terminals
            Terminal number = new NumberLiteral("NUMBER");
            VariableIdentifierTerminal variable = new VariableIdentifierTerminal();
            Terminal stringLiteral = new StringLiteral("STRING", "\"", ScanFlags.None);
            //Important: do not add comment term to base.NonGrammarTerminals list - we do use this terminal in grammar rules
            Terminal comment = new CommentTerminal("Comment", "REM", "\n");

            Terminal comma = Symbol(",", "comma");

            // Make sure reserved keywords of the BASIC language aren't mistaken
            // for variables. Only the keywords ending with '$' could be mistaken
            // for variables.
            variable.AddKeywords(
                "inkey$", "left$", "right$", "mid$", "chr$",
                "space$", "str$", "string$"
            );

            // Define the non-terminals
            NonTerminal PROGRAM = new NonTerminal("PROGRAM", typeof(ProgramNode));
            NonTerminal LINE = new NonTerminal("LINE", typeof(LineNode));
            NonTerminal STATEMENT_LIST = new NonTerminal("STATEMENT_LIST", typeof(StatementListNode));
            NonTerminal STATEMENT = new NonTerminal("STATEMENT", typeof(StatementNode));
            NonTerminal COMMAND = new NonTerminal("COMMAND", typeof(StatementNode)); //TODO: create command node
            NonTerminal PRINT_STMT = new NonTerminal("PRINT_STMT", typeof(PrintStmtNode));
            NonTerminal INPUT_STMT = new NonTerminal("INPUT_STMT", typeof(InputStmtNode));
            NonTerminal IF_STMT = new NonTerminal("IF_STMT", typeof(IfElseStmtNode)); //TODO: join IfStmtNode and IfElseStmtNode in one
            NonTerminal ELSE_CLAUSE_OPT = new NonTerminal("ELSE_CLAUSE_OPT", typeof(GenericJsBasicNode));
            NonTerminal EXPR = new NonTerminal("EXPRESSION", typeof(ExpressionNode));
            NonTerminal EXPR_LIST = new NonTerminal("EXPRESSION_LIST", typeof(ExprListNode));
            NonTerminal BINARY_OP = new NonTerminal("BINARY_OP", typeof(BinaryOpNode));
            NonTerminal BINARY_EXPR = new NonTerminal("BINARY_EXPR", typeof(GenericJsBasicNode)); //TODO: create Binary_expr node
            NonTerminal BRANCH_STMT = new NonTerminal("BRANCH_STMT", typeof(BranchStmtNode));
            NonTerminal ASSIGN_STMT = new NonTerminal("ASSIGN_STMT", typeof(AssignStmtNode));
            NonTerminal FOR_STMT = new NonTerminal("FOR_STMT", typeof(ForStmtNode));
            NonTerminal STEP_OPT = new NonTerminal("STEP_OPT", typeof(GenericJsBasicNode));  //TODO: create step specifier node
            NonTerminal NEXT_STMT = new NonTerminal("NEXT_STMT", typeof(NextStmtNode));
            NonTerminal LOCATE_STMT = new NonTerminal("LOCATE_STMT", typeof(LocateStmtNode));
            NonTerminal WHILE_STMT = new NonTerminal("WHILE_STMT", typeof(WhileStmtNode));
            NonTerminal WEND_STMT = new NonTerminal("WEND_STMT", typeof(WendStmtNode));
            NonTerminal SWAP_STMT = new NonTerminal("SWAP_STMT", typeof(SwapStmtNode));
            NonTerminal GLOBAL_FUNCTION_EXPR = new NonTerminal("GLOBAL_FUNCTION_EXPR", typeof(GlobalFunctionExpr));
            NonTerminal ARG_LIST = new NonTerminal("ARG_LIST", typeof(GenericJsBasicNode));
            NonTerminal FUNC_NAME = new NonTerminal("FUNC_NAME", typeof(GenericJsBasicNode));
            NonTerminal COMMENT_STMT = new NonTerminal("COMMENT_STMT", typeof(RemStmtNode));
            NonTerminal GLOBAL_VAR_EXPR = new NonTerminal("GLOBAL_VAR_EXPR", typeof(GenericJsBasicNode));

            // Set the PROGRAM to be the root node of BASIC programs.
            // A program is a bunch of lines
            this.Root = PROGRAM;

            #endregion

            #region Grammar declaration

            // A program is a collection of lines
            PROGRAM.Rule = MakePlusRule(PROGRAM, null, LINE);

            // A line can be an empty line, or it's a number followed by a statement list ended by a new-line.
            LINE.Rule = NewLine | number + NewLine | number + STATEMENT_LIST + NewLine;

            // A statement list is 1 or more statements separated by the ':' character
            STATEMENT_LIST.Rule = MakePlusRule(STATEMENT_LIST, Symbol(":"), STATEMENT);

            // A statement can be one of a number of types
            STATEMENT.Rule = EXPR | ASSIGN_STMT | PRINT_STMT | INPUT_STMT | IF_STMT | COMMENT_STMT
                                | BRANCH_STMT | COMMAND | FOR_STMT | NEXT_STMT | LOCATE_STMT | SWAP_STMT
                                | WHILE_STMT | WEND_STMT;
            // The different statements are defined here
            PRINT_STMT.Rule = "print" + EXPR_LIST;
            INPUT_STMT.Rule = "input" + EXPR_LIST + variable;
            IF_STMT.Rule = "if" + EXPR + "then" + STATEMENT_LIST + ELSE_CLAUSE_OPT;
            ELSE_CLAUSE_OPT.Rule = Empty | "else" + STATEMENT_LIST;
            BRANCH_STMT.Rule = "goto" + number | "gosub" + number | "return";
            ASSIGN_STMT.Rule = variable + "=" + EXPR;
            LOCATE_STMT.Rule = "locate" + EXPR + comma + EXPR;
            SWAP_STMT.Rule = "swap" + EXPR + comma + EXPR;
            COMMAND.Rule = Symbol("end") | "cls";
            COMMENT_STMT.Rule = comment;

            // An expression is a number, or a variable, a string, or the result of a binary comparison.
            EXPR.Rule = number | variable | stringLiteral | BINARY_EXPR
                      | GLOBAL_VAR_EXPR | GLOBAL_FUNCTION_EXPR | "(" + EXPR + ")";
            BINARY_EXPR.Rule = EXPR + BINARY_OP + EXPR;

            BINARY_OP.Rule = Symbol("+") | "-" | "*" | "/" | "=" | "<=" | ">=" | "<" | ">" | "<>" | "and" | "or";
            //let's do operator precedence right here
            RegisterOperators(50, "*", "/");
            RegisterOperators(40, "+", "-");
            RegisterOperators(30, "=", "<=", ">=", "<", ">", "<>");
            RegisterOperators(20, "and", "or");

            // Used by print and input to allow a bunch of expressions separated by whitespace,
            // or be empty, for example:
            // print
            // print "Hi"
            // print "Hi " a$
            // All of these match "print" EXPR_LIST
            EXPR_LIST.Rule = MakeStarRule(EXPR_LIST, null, EXPR);

            FOR_STMT.Rule = "for" + ASSIGN_STMT + "to" + EXPR + STEP_OPT;
            STEP_OPT.Rule = Empty | "step" + number;
            NEXT_STMT.Rule = "next" + variable;
            WHILE_STMT.Rule = "while" + EXPR;
            WEND_STMT.Rule = "wend";

            //TODO: check number of arguments for particular function in node constructor
            GLOBAL_FUNCTION_EXPR.Rule = FUNC_NAME + "(" + ARG_LIST + ")";
            FUNC_NAME.Rule = Symbol("len") | "left$" | "mid$" | "right$" | "abs" | "asc" | "chr$" | "csrlin$"
                           | "cvi" | "cvs" | "cvd" | "exp" | "fix" | "log" | "pos" | "sgn" | "sin" | "cos" | "tan"
                           | "instr" | "space$" | "spc" | "sqr" | "str$" | "string$" | "val" | "cint";
            ARG_LIST.Rule = MakePlusRule(ARG_LIST, comma, EXPR);

            GLOBAL_VAR_EXPR.Rule = Symbol("rnd") | "timer" | "inkey$" | "csrlin";

            // By registering these strings as "punctuation", we exclude them from
            // appearing in as nodes in the compiled node tree.
            RegisterPunctuation("(", ")", ",");

            #endregion
        }
 //http://www.microsoft.com/downloads/details.aspx?FamilyId=6D50D709-EAA4-44D7-8AF3-E14280403E6E&displaylang=en section 2
 public static NumberLiteral CreateVbNumber(string name)
 {
     NumberLiteral term = new NumberLiteral(name, TermOptions.EnableQuickParse | TermOptions.SpecialIgnoreCase);
       term.DefaultIntTypes = new TypeCode[] { TypeCode.Int32, TypeCode.Int64 };
       //term.DefaultFloatType = TypeCode.Double; it is default
       term.AddPrefixFlag("&H", ScanFlags.Hex);
       term.AddPrefixFlag("&O", ScanFlags.Octal);
       term.AddSuffixCodes("S", TypeCode.Int16);
       term.AddSuffixCodes("I", TypeCode.Int32);
       term.AddSuffixCodes("%", TypeCode.Int32);
       term.AddSuffixCodes("L", TypeCode.Int64);
       term.AddSuffixCodes("&", TypeCode.Int64);
       term.AddSuffixCodes("D", TypeCode.Decimal);
       term.AddSuffixCodes("@", TypeCode.Decimal);
       term.AddSuffixCodes("F", TypeCode.Single);
       term.AddSuffixCodes("!", TypeCode.Single);
       term.AddSuffixCodes("R", TypeCode.Double);
       term.AddSuffixCodes("#", TypeCode.Double);
       term.AddSuffixCodes("US", TypeCode.UInt16);
       term.AddSuffixCodes("UI", TypeCode.UInt32);
       term.AddSuffixCodes("UL", TypeCode.UInt64);
       return term;
 }
 //Note - this is incomplete implementation; need to add functionality to NumberTerminal class to support type detection based
 // on exponent symbol.
 // From R6RS:
 //  ... representations of number objects may be written with an exponent marker that indicates the desired precision
 // of the inexact representation. The letters s, f, d, and l specify the use of short, single, double, and long precision, respectively.
 public static NumberLiteral CreateSchemeNumber(string name)
 {
     NumberLiteral term = new NumberLiteral(name, TermOptions.EnableQuickParse | TermOptions.SpecialIgnoreCase);
       term.DefaultIntTypes = new TypeCode[] { TypeCode.Int32, TypeCode.Int64, NumberLiteral.TypeCodeBigInt };
       term.DefaultFloatType = TypeCode.Double; // it is default
       term.ExponentSymbols = "sfdl";
       term.AddPrefixFlag("#b", ScanFlags.Binary);
       term.AddPrefixFlag("#o", ScanFlags.Octal);
       term.AddPrefixFlag("#x", ScanFlags.Hex);
       term.AddPrefixFlag("#d", ScanFlags.None);
       term.AddPrefixFlag("#i", ScanFlags.None); // inexact prefix, has no effect
       term.AddPrefixFlag("#e", ScanFlags.None); // exact prefix, has no effect
       term.AddSuffixCodes("J", NumberLiteral.TypeCodeImaginary);
       return term;
 }
 //http://docs.python.org/ref/numbers.html
 public static NumberLiteral CreatePythonNumber(string name)
 {
     NumberLiteral term = new NumberLiteral(name, TermOptions.EnableQuickParse | TermOptions.SpecialIgnoreCase | TermOptions.NumberAllowStartEndDot);
       //default int types are Integer (32bit) -> LongInteger (BigInt); Try Int64 before BigInt: Better performance?
       term.DefaultIntTypes = new TypeCode[] { TypeCode.Int32, TypeCode.Int64, NumberLiteral.TypeCodeBigInt };
       // term.DefaultFloatType = TypeCode.Double; -- it is default
       //float type is implementation specific, thus try decimal first (higher precision)
       //term.DefaultFloatTypes = new TypeCode[] { TypeCode.Decimal, TypeCode.Double };
       term.AddPrefixFlag("0x", ScanFlags.Hex);
       term.AddPrefixFlag("0", ScanFlags.Octal);
       term.AddSuffixCodes("L", TypeCode.Int64, NumberLiteral.TypeCodeBigInt);
       term.AddSuffixCodes("J", NumberLiteral.TypeCodeImaginary);
       return term;
 }
        public FlGrammar()
        {
            // turn off case sensitivity
            CaseSensitive = false;

            // define all the non-terminals
            var program = new NonTerminal("program", typeof(ProgramNode));
            var statementList = new NonTerminal("statementList", typeof(StatementNode));
            var statement = new NonTerminal("statement", typeof(SkipNode));
            var expression = new NonTerminal("expression", typeof(ExpressionNode));
            var binaryOperator = new NonTerminal("binaryOperator", typeof(SkipNode));

            var variableDeclaration = new NonTerminal("variableDeclaration", typeof(VariableDeclarationNode));
            var variableAssignment = new NonTerminal("variableAssignment", typeof(VariableAssignmentNode));

            var ifStatement = new NonTerminal("ifStatement", typeof(IfStatementNode));
            var elseStatement = new NonTerminal("elseStatement", typeof(ElseStatementNode));

            // define all the terminals
            var variable = new IdentifierTerminal("variable");
            variable.AddKeywords("set", "var" , "to", "if", "freight", "cost", "is", "loop", "through", "order");
            var number = new NumberLiteral("number");
            var stringLiteral = new StringLiteral("string", "\"", ScanFlags.None);

            RegisterPunctuation(";", "[", "]", "(", ")");
            //var lpar = new Terminal("(");
            //var rpar = new Terminal(")");
            //var lbr = new Terminal("{");
            //var rbr = new Terminal("}");


            // specify the non-terminal which is the root of the AST
            //Root = program;
            //binaryOperator.Rule = Symbol("+") | "-" | "*" | "/" | "<" | "==" | "!=" | ">" | "<=" | ">=" | "is";
            //program.Rule = statementList;
            //statementList.Rule = MakeStarRule(statementList, null, statement);
            //statement.Rule = variableDeclaration + ";" | variableAssignment + ";" | expression + ";" | ifStatement;
            //variableAssignment.Rule = variable + "=" + expression;
            //variableDeclaration.Rule = Symbol("var") + variable;
            //ifStatement.Rule = Symbol("if") + "(" + expression + ")"
            //    + "{" + expression + "}"
            //    + elseStatement;
            //elseStatement.Rule = Empty | "else" + "{" + expression + "}";
            //expression.Rule = number | variable | stringLiteral
            //    | expression + binaryOperator + expression
            //    | "(" + expression + ")";
            Root = program;
            binaryOperator.Rule = Symbol("+") | "-" | "*" | "/" | "<" | "==" | "!=" | ">" | "<=" | ">=" | "is";


            program.Rule = statementList;
            statementList.Rule = MakeStarRule(statementList, null, statement);
            statement.Rule = variableDeclaration + ";" | variableAssignment + ";" | expression + ";" | ifStatement;

            variableAssignment.Rule = variable + "=" + expression;
            variableDeclaration.Rule = Symbol("var") + variable;

            ifStatement.Rule = "if" + Symbol("(") + expression + Symbol(")")
                               + Symbol("{") + statementList + Symbol("}")
                               + elseStatement;
            elseStatement.Rule = Empty | "else" + Symbol("{") + statementList + Symbol("}");

            expression.Rule = number | variable | stringLiteral
                | expression + binaryOperator + expression
                | "(" + expression + ")";
        }