Esempio n. 1
0
 private NumberLiteral CreateNumberLiteral(string name)
 {
     var term = new NumberLiteral(name);
     term.DefaultIntTypes = new TypeCode[] { TypeCode.Int32, TypeCode.Int64 };
     term.DefaultFloatType = TypeCode.Double;
     term.AddPrefix("0x", NumberOptions.Hex);
     term.AddSuffix("l", TypeCode.Int64, TypeCode.UInt64);
     term.AddSuffix("f", TypeCode.Single);
     term.AddSuffix("d", TypeCode.Double);
     return term;
 }
Esempio n. 2
0
        private NumberLiteral CreateNumberLiteral(string name)
        {
            var term = new NumberLiteral(name);

            term.DefaultIntTypes  = new TypeCode[] { TypeCode.Int32, TypeCode.Int64 };
            term.DefaultFloatType = TypeCode.Double;
            term.AddPrefix("0x", NumberOptions.Hex);
            term.AddSuffix("l", TypeCode.Int64, TypeCode.UInt64);
            term.AddSuffix("f", TypeCode.Single);
            term.AddSuffix("d", TypeCode.Double);
            return(term);
        }
		public static NumberLiteral CreateJavaNumber(string name)
		{
			var term = new NumberLiteral(name, NumberOptions.AllowStartEndDot)
			{
				DefaultIntTypes = new[] { TypeCode.Int32 },
				DefaultFloatType = TypeCode.Double
			};
			term.AddPrefix("0x", NumberOptions.Hex);
			term.AddSuffix("l", TypeCode.Int64);
			term.AddSuffix("f", TypeCode.Single);
			term.AddSuffix("d", TypeCode.Double);
			return term;
		}
Esempio n. 4
0
        public static NumberLiteral CreateJavaNumber(string name)
        {
            var term = new NumberLiteral(name, NumberOptions.AllowStartEndDot)
            {
                DefaultIntTypes  = new[] { TypeCode.Int32 },
                DefaultFloatType = TypeCode.Double
            };

            term.AddPrefix("0x", NumberOptions.Hex);
            term.AddSuffix("l", TypeCode.Int64);
            term.AddSuffix("f", TypeCode.Single);
            term.AddSuffix("d", TypeCode.Double);
            return(term);
        }
Esempio n. 5
0
		//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);
      term.DefaultIntTypes = new TypeCode[] { TypeCode.Int32, TypeCode.UInt32, TypeCode.Int64, TypeCode.UInt64 };
      term.DefaultFloatType = TypeCode.Double;
      term.AddPrefix("0x", NumberOptions.Hex);
      term.AddSuffix("u", TypeCode.UInt32, TypeCode.UInt64);
      term.AddSuffix("l", TypeCode.Int64, TypeCode.UInt64);
      term.AddSuffix("ul", TypeCode.UInt64);
      term.AddSuffix("f", TypeCode.Single);
			term.AddSuffix("d", TypeCode.Double);
      term.AddSuffix("m", TypeCode.Decimal);
      return term;
    }
Esempio n. 6
0
        static NumberLiteral CreateNumberLiteral(string name)
        {
            NumberLiteral term = new NumberLiteral(name);

            term.DefaultIntTypes  = new TypeCode[] { TypeCode.Int32, TypeCode.UInt32, TypeCode.Int64, TypeCode.UInt64 };
            term.DefaultFloatType = TypeCode.Double;
            term.AddPrefix("0x", NumberOptions.Hex);
            term.AddSuffix("u", TypeCode.UInt32, TypeCode.UInt64);
            term.AddSuffix("l", TypeCode.Int64, TypeCode.UInt64);
            term.AddSuffix("ul", TypeCode.UInt64);
            term.AddSuffix("f", TypeCode.Single);
            term.AddSuffix("d", TypeCode.Double);
            term.AddSuffix("m", TypeCode.Decimal);
            term.Options |= NumberOptions.AllowSign;
            return(term);
        }
Esempio n. 7
0
        public GLSLGrammar()
        {
            var multiLine = new CommentTerminal ("MULTI-LINE", "/*", "*/");
            this.NonGrammarTerminals.Add (multiLine);

            string RIGHT_PAREN = ")";
            string LEFT_BRACE = "{";
            string RIGHT_BRACE = "}";
            string SEMICOLON = ";";
            var COMMA = ToTerm(",", "COMMA");
            var QUESTION = ToTerm ("?", "QUESTION");
            var COLON = ToTerm (":", "COLON");
            var OR_OP = ToTerm ("||", "OR_OP");
            var XOR_OP = ToTerm ("^^", "XOR_OP");
            var AND_OP = ToTerm ("&&", "AND_OP");
            var VERTICAL_BAR = ToTerm ("|", "VERTICAL_BAR");
            var CARET = ToTerm ("^", "CARET");
            var AMPERSAND = ToTerm ("&", "AMPERSAND");
            var EQ_OP = ToTerm ("==","EQ_OP");
            var NE_OP = ToTerm ("!=","NE_OP");
            var LEFT_ANGLE = ToTerm ("<","LEFT_ANGLE");
            var RIGHT_ANGLE = ToTerm (">","RIGHT_ANGLE");
            var LE_OP = ToTerm ("<=","LE_OP");
            var GE_OP = ToTerm (">=", "GE_OP");
            var LEFT_OP = ToTerm ("<<","LEFT_OP");
            var RIGHT_OP = ToTerm (">>", "RIGHT_OP");
            var PLUS = ToTerm ("+", "PLUS");
            var DASH = ToTerm ("-", "DASH");
            var STAR = ToTerm ("*", "STAR");
            var SLASH = ToTerm ("/", "SLASH");
            var PERCENT = ToTerm ("%", "PERCENT");
            var INC_OP = ToTerm ("++", "INC_OP");
            var DEC_OP = ToTerm ("--", "DEC_OP");
            var BANG = ToTerm ("!", "BANG");
            var TILDE = ToTerm ("~", "TILDE");
            var EQUAL = ToTerm ("=", "EQUAL");
            var MUL_ASSIGN = ToTerm ("*=", "MUL_ASSIGN");
            var DIV_ASSIGN = ToTerm ("/=", "DIV_ASSIGN");
            var MOD_ASSIGN = ToTerm ("%=", "MOD_ASSIGN");
            var ADD_ASSIGN = ToTerm ("+=", "ADD_ASSIGN");
            var SUB_ASSIGN = ToTerm ("-=", "SUB_ASSIGN");
            var LEFT_ASSIGN = ToTerm ("<<=", "LEFT_ASSIGN");
            var RIGHT_ASSIGN = ToTerm (">>=", "RIGHT_ASSIGN");
            var AND_ASSIGN = ToTerm ("&=", "AND_ASSIGN");
            var XOR_ASSIGN = ToTerm ("^=", "XOR_ASSIGN");
            var OR_ASSIGN = ToTerm ("|=", "OR_ASSIGN");
            var IF_STM = ToTerm ("if", "IF");
            string LEFT_PAREN = "(";
            var ELSE = ToTerm ("else", "ELSE");
            var SWITCH_STM = ToTerm ("switch", "SWITCH");
            var CASE_STM = ToTerm ("case", "CASE");
            var DEFAULT_STM = ToTerm ("default", "DEFAULT");
            var WHILE = ToTerm ("while", "WHILE");
            var FOR_STM = ToTerm ("for", "FOR");
            var DO_STM = ToTerm ("do", "DO");
            IDENTIFIER = TerminalFactory.CreateCSharpIdentifier("IDENTIFIER");

            var VOID_STM = ToTerm ("void", "VOID");
            var FLOAT_TKN = ToTerm ("float", "FLOAT");
            var DOUBLE_TKN = ToTerm ("double", "DOUBLE");
            var INT_TKN = ToTerm ("int", "INT");
            var UINT_TKN = ToTerm ("uint", "UINT");
            var BOOL_TKN = ToTerm ("bool", "BOOL");
            var VEC2 = ToTerm ("vec2", "VEC2");
            var VEC3 = ToTerm ("vec3", "VEC3");
            var VEC4 = ToTerm ("vec4", "VEC4");
            var DVEC2 = ToTerm ("dvec2", "DVEC2");
            var DVEC3 = ToTerm ("dvec3", "DVEC3");
            var DVEC4 = ToTerm ("dvec4", "DVEC4");
            var BVEC2 = ToTerm ("bvec2", "BVEC2");
            var BVEC3 = ToTerm ("bvec3", "BVEC3");
            var BVEC4 = ToTerm ("bvec4", "BVEC4");
            var IVEC2 = ToTerm ("ivec2", "IVEC2");
            var IVEC3 = ToTerm ("ivec3", "IVEC3");
            var IVEC4 = ToTerm ("ivec4", "IVEC4");
            var UVEC2 = ToTerm ("uvec2", "UVEC2");
            var UVEC3 = ToTerm ("uvec3", "UVEC3");
            var UVEC4 = ToTerm ("uvec4", "UVEC4");
            var MAT2 = ToTerm ("mat2", "MAT2");
            var MAT3 = ToTerm ("mat3", "MAT3");
            var MAT4 = ToTerm ("mat4", "MAT4");
            var MAT2X2 = ToTerm ("mat2x2", "MAT2X2");
            var MAT2X3 = ToTerm ("mat2x3", "MAT2X3");
            var MAT2X4 = ToTerm ("mat2x4", "MAT2X4");
            var MAT3X2 = ToTerm ("mat3x2", "MAT3X2");
            var MAT3X3 = ToTerm ("mat3x3", "MAT3X3");
            var MAT3X4 = ToTerm ("mat3x4", "MAT3X4");
            var MAT4X2 = ToTerm ("mat4x2", "MAT4X2");
            var MAT4X3 = ToTerm ("mat4x3", "MAT4X3");
            var MAT4X4 = ToTerm ("mat4x4", "MAT4X4");
            var DMAT2 = ToTerm ("dmat2", "DMAT2");
            var DMAT3 = ToTerm ("dmat3", "DMAT3");
            var DMAT4 = ToTerm ("dmat4", "DMAT4");
            var DMAT2X2 = ToTerm ("dmat2x2", "DMAT2X2");
            var DMAT2X3 = ToTerm ("dmat2x3", "DMAT2X3");
            var DMAT2X4 = ToTerm ("dmat2x4", "DMAT2X4");
            var DMAT3X2 = ToTerm ("dmat3x2", "DMAT3X2");
            var DMAT3X3 = ToTerm ("dmat3x3", "DMAT3X3");
            var DMAT3X4 = ToTerm ("dmat3x4", "DMAT3X4");
            var DMAT4X2 = ToTerm ("dmat4x2", "DMAT4X2");
            var DMAT4X3 = ToTerm ("dmat4x3", "DMAT4X3");
            var DMAT4X4 = ToTerm ("dmat4x4", "DMAT4X4");
            var ATOMIC_UINT = ToTerm ("atomic_uint", "ATOMIC_UINT");
            var SAMPLER1D = ToTerm ("sampler1d", "SAMPLER1D");
            var SAMPLER2D = ToTerm ("sampler2d", "SAMPLER2D");
            var SAMPLER3D = ToTerm ("sampler3d", "SAMPLER3D");
            var SAMPLERCUBE = ToTerm ("samplercube", "SAMPLERCUBE");
            var SAMPLER1DSHADOW = ToTerm ("sampler1dshadow", "SAMPLER1DSHADOW");
            var SAMPLER2DSHADOW = ToTerm ("sampler2dshadow", "SAMPLER2DSHADOW");
            var SAMPLERCUBESHADOW = ToTerm ("samplercubeshadow", "SAMPLERCUBESHADOW");
            var SAMPLER1DARRAY = ToTerm ("sampler1darray", "SAMPLER1DARRAY");
            var SAMPLER2DARRAY = ToTerm ("sampler2darray", "SAMPLER2DARRAY");
            var SAMPLER1DARRAYSHADOW = ToTerm ("sampler1darrayshadow", "SAMPLER1DARRAYSHADOW");
            var SAMPLER2DARRAYSHADOW = ToTerm ("sampler2darrayshadow", "SAMPLER2DARRAYSHADOW");
            var SAMPLERCUBEARRAY = ToTerm ("samplercubearray", "SAMPLERCUBEARRAY");
            var SAMPLERCUBEARRAYSHADOW = ToTerm ("samplercubearrayshadow", "SAMPLERCUBEARRAYSHADOW");
            var ISAMPLER1D = ToTerm ("isampler1d", "ISAMPLER1D");
            var ISAMPLER2D = ToTerm ("isampler2d", "ISAMPLER2D");
            var ISAMPLER3D = ToTerm ("isampler3d", "ISAMPLER3D");
            var ISAMPLERCUBE = ToTerm ("isamplercube", "ISAMPLERCUBE");
            var ISAMPLER1DARRAY = ToTerm ("isampler1darray", "ISAMPLER1DARRAY");
            var ISAMPLER2DARRAY = ToTerm ("isampler2darray", "ISAMPLER2DARRAY");
            var ISAMPLERCUBEARRAY = ToTerm ("isamplercubearray", "ISAMPLERCUBEARRAY");
            var USAMPLER1D = ToTerm ("usampler1d", "USAMPLER1D");
            var USAMPLER2D = ToTerm ("usampler2d", "USAMPLER2D");
            var USAMPLER3D = ToTerm ("usampler3d", "USAMPLER3D");
            var USAMPLERCUBE = ToTerm ("usamplercube", "USAMPLERCUBE");
            var USAMPLER1DARRAY = ToTerm ("usampler1darray", "USAMPLER1DARRAY");
            var USAMPLER2DARRAY = ToTerm ("usampler2darray", "USAMPLER2DARRAY");
            var USAMPLERCUBEARRAY = ToTerm ("usamplercubearray", "USAMPLERCUBEARRAY");
            var SAMPLER2DRECT = ToTerm ("sampler2drect", "SAMPLER2DRECT");
            var SAMPLER2DRECTSHADOW = ToTerm ("sampler2drectshadow", "SAMPLER2DRECTSHADOW");
            var ISAMPLER2DRECT = ToTerm ("isampler2drect", "ISAMPLER2DRECT");
            var USAMPLER2DRECT = ToTerm ("usampler2drect", "USAMPLER2DRECT");
            var SAMPLERBUFFER = ToTerm ("samplerbuffer", "SAMPLERBUFFER");
            var ISAMPLERBUFFER = ToTerm ("isamplerbuffer", "ISAMPLERBUFFER");
            var USAMPLERBUFFER = ToTerm ("usamplerbuffer", "USAMPLERBUFFER");
            var SAMPLER2DMS = ToTerm ("sampler2dms", "SAMPLER2DMS");
            var ISAMPLER2DMS = ToTerm ("isampler2dms", "ISAMPLER2DMS");
            var USAMPLER2DMS = ToTerm ("usampler2dms", "USAMPLER2DMS");
            var SAMPLER2DMSARRAY = ToTerm ("sampler2dmsarray", "SAMPLER2DMSARRAY");
            var ISAMPLER2DMSARRAY = ToTerm ("isampler2dmsarray", "ISAMPLER2DMSARRAY");
            var USAMPLER2DMSARRAY = ToTerm ("usampler2dmsarray", "USAMPLER2DMSARRAY");
            var IMAGE1D = ToTerm ("image1d", "IMAGE1D");
            var IIMAGE1D = ToTerm ("iimage1d", "IIMAGE1D");
            var UIMAGE1D = ToTerm ("uimage1d", "UIMAGE1D");
            var IMAGE2D = ToTerm ("image2d", "IMAGE2D");
            var IIMAGE2D = ToTerm ("iimage2d", "IIMAGE2D");
            var UIMAGE2D = ToTerm ("uimage2d", "UIMAGE2D");
            var IMAGE3D = ToTerm ("image3d", "IMAGE3D");
            var IIMAGE3D = ToTerm ("iimage3d", "IIMAGE3D");
            var UIMAGE3D = ToTerm ("uimage3d", "UIMAGE3D");
            var IMAGE2DRECT = ToTerm ("image2drect", "IMAGE2DRECT");
            var IIMAGE2DRECT = ToTerm ("iimage2drect", "IIMAGE2DRECT");
            var UIMAGE2DRECT = ToTerm ("uimage2drect", "UIMAGE2DRECT");
            var IMAGECUBE = ToTerm ("imagecube", "IMAGECUBE");
            var IIMAGECUBE = ToTerm ("iimagecube", "IIMAGECUBE");
            var UIMAGECUBE = ToTerm ("uimagecube", "UIMAGECUBE");
            var IMAGEBUFFER = ToTerm ("imagebuffer", "IMAGEBUFFER");
            var IIMAGEBUFFER = ToTerm ("iimagebuffer", "IIMAGEBUFFER");
            var UIMAGEBUFFER = ToTerm ("uimagebuffer", "UIMAGEBUFFER");
            var IMAGE1DARRAY  = ToTerm ("image1darray", "IMAGE1DARRAY");
            var IIMAGE1DARRAY = ToTerm ("iimage1darray", "IIMAGE1DARRAY");
            var UIMAGE1DARRAY = ToTerm ("uimage1darray", "UIMAGE1DARRAY");
            var IMAGE2DARRAY = ToTerm ("image2darray", "IMAGE2DARRAY");
            var IIMAGE2DARRAY = ToTerm ("iimage2darray", "IIMAGE2DARRAY");
            var UIMAGE2DARRAY = ToTerm ("uimage2darray", "UIMAGE2DARRAY");
            var IMAGECUBEARRAY = ToTerm ("imagecubearray", "IMAGECUBEARRAY");
            var IIMAGECUBEARRAY = ToTerm ("iimagecubearray", "IIMAGECUBEARRAY");
            var UIMAGECUBEARRAY = ToTerm ("uimagecubearray", "UIMAGECUBEARRAY");
            var IMAGE2DMS = ToTerm ("image2dms", "IMAGE2DMS");
            var IIMAGE2DMS = ToTerm ("iimage2dms", "IIMAGE2DMS");
            var UIMAGE2DMS = ToTerm ("uimage2dms", "UIMAGE2DMS");
            var IMAGE2DMSARRAY = ToTerm ("image2dmsarray", "IMAGE2DMSARRAY");
            var IIMAGE2DMSARRAY = ToTerm ("iimage2dmsarray", "IIMAGE2DMSARRAY");
            var UIMAGE2DMSARRAY = ToTerm ("uimage2dmsarray", "UIMAGE2DMSARRAY");
            var SAMPLEREXTERNALOES = ToTerm ("samplerexternaloes", "SAMPLEREXTERNALOES");

            TypesTerms = new HashSet<KeyTerm> (
                new []{
                VOID_STM
                ,FLOAT_TKN
                ,DOUBLE_TKN
                ,INT_TKN
                ,UINT_TKN
                ,BOOL_TKN
                ,VEC2
                ,VEC3
                ,VEC4
                ,DVEC2
                ,DVEC3
                ,DVEC4
                ,BVEC2
                ,BVEC3
                ,BVEC4
                ,IVEC2
                ,IVEC3
                ,IVEC4
                ,UVEC2
                ,UVEC3
                ,UVEC4
                ,MAT2
                ,MAT3
                ,MAT4
                ,MAT2X2
                ,MAT2X3
                ,MAT2X4
                ,MAT3X2
                ,MAT3X3
                ,MAT3X4
                ,MAT4X2
                ,MAT4X3
                ,MAT4X4
                ,DMAT2
                ,DMAT3
                ,DMAT4
                ,DMAT2X2
                ,DMAT2X3
                ,DMAT2X4
                ,DMAT3X2
                ,DMAT3X3
                ,DMAT3X4
                ,DMAT4X2
                ,DMAT4X3
                ,DMAT4X4
                ,ATOMIC_UINT
                ,SAMPLER1D
                ,SAMPLER2D
                ,SAMPLER3D
                ,SAMPLERCUBE
                ,SAMPLER1DSHADOW
                ,SAMPLER2DSHADOW
                ,SAMPLERCUBESHADOW
                ,SAMPLER1DARRAY
                ,SAMPLER2DARRAY
                ,SAMPLER1DARRAYSHADOW
                ,SAMPLER2DARRAYSHADOW
                ,SAMPLERCUBEARRAY
                ,SAMPLERCUBEARRAYSHADOW
                ,ISAMPLER1D
                ,ISAMPLER2D
                ,ISAMPLER3D
                ,ISAMPLERCUBE
                ,ISAMPLER1DARRAY
                ,ISAMPLER2DARRAY
                ,ISAMPLERCUBEARRAY
                ,USAMPLER1D
                ,USAMPLER2D
                ,USAMPLER3D
                ,USAMPLERCUBE
                ,USAMPLER1DARRAY
                ,USAMPLER2DARRAY
                ,USAMPLERCUBEARRAY
                ,SAMPLER2DRECT
                ,SAMPLER2DRECTSHADOW
                ,ISAMPLER2DRECT
                ,USAMPLER2DRECT
                ,SAMPLERBUFFER
                ,ISAMPLERBUFFER
                ,USAMPLERBUFFER
                ,SAMPLER2DMS
                ,ISAMPLER2DMS
                ,USAMPLER2DMS
                ,SAMPLER2DMSARRAY
                ,ISAMPLER2DMSARRAY
                ,USAMPLER2DMSARRAY
                ,IMAGE1D
                ,IIMAGE1D
                ,UIMAGE1D
                ,IMAGE2D
                ,IIMAGE2D
                ,UIMAGE2D
                ,IMAGE3D
                ,IIMAGE3D
                ,UIMAGE3D
                ,IMAGE2DRECT
                ,IIMAGE2DRECT
                ,UIMAGE2DRECT
                ,IMAGECUBE
                ,IIMAGECUBE
                ,UIMAGECUBE
                ,IMAGEBUFFER
                ,IIMAGEBUFFER
                ,UIMAGEBUFFER
                ,IMAGE1DARRAY
                ,IIMAGE1DARRAY
                ,UIMAGE1DARRAY
                ,IMAGE2DARRAY
                ,IIMAGE2DARRAY
                ,UIMAGE2DARRAY
                ,IMAGECUBEARRAY
                ,IIMAGECUBEARRAY
                ,UIMAGECUBEARRAY
                ,IMAGE2DMS
                ,IIMAGE2DMS
                ,UIMAGE2DMS
                ,IMAGE2DMSARRAY
                ,IIMAGE2DMSARRAY
                ,UIMAGE2DMSARRAY
                ,SAMPLEREXTERNALOES
                });

            var TYPE_NAME = TerminalFactory.CreateCSharpString ("TYPE_NAME");
            string LEFT_BRACKET = "[";
            string RIGHT_BRACKET = "]";
            var STRUCT_STM = ToTerm ("struct", "STRUCT");
            var CONTINUE = ToTerm ("continue", "CONTINUE");
            var BREAK_STM = ToTerm ("break", "BREAK");
            var RETURN_STM = ToTerm ("return", "RETURN");
            var DISCARD = ToTerm ("discard", "DISCARD");
            var PRECISION = ToTerm ("precision", "PRECISION");
            var HIGH_PRECISION = ToTerm ("highp", "HIGH_PRECISION");
            var MEDIUM_PRECISION = ToTerm ("mediump", "MEDIUM_PRECISION");
            var LOW_PRECISION = ToTerm ("lowp", "LOW_PRECISION");
            FIELD_SELECTION = TerminalFactory.CreateCSharpIdentifier ("FIELD_SELECTION");
            INTCONSTANT = new NumberLiteral ("INTCONSTANT", NumberOptions.AllowSign | NumberOptions.IntOnly);
            INTCONSTANT.Priority = 4;
            var DOT = new NonTerminal("DOT");
            DOT.Rule = ".";
            DOT.Precedence = 1;
            var UINTCONSTANT = new NumberLiteral ("UINTCONSTANT", NumberOptions.IntOnly);
            UINTCONSTANT.Priority = 3;
            FLOATCONSTANT = new NumberLiteral ("FLOATCONSTANT", NumberOptions.AllowSign | NumberOptions.AllowLetterAfter);
            FLOATCONSTANT.Priority = 2;
            FLOATCONSTANT.AddSuffix("f", TypeCode.Single);
            REMAINDER = new NumberLiteral ("REMAINDER", NumberOptions.NoDotAfterInt | NumberOptions.IntOnly | NumberOptions.AllowLetterAfter);
            REMAINDER.AddSuffix("f", TypeCode.Single);
            var DOUBLECONSTANT = new NumberLiteral ("DOUBLECONSTANT", NumberOptions.AllowSign);
            var FALSE_STM = ToTerm ("false", "FALSE_STM");
            var TRUE_STM = ToTerm ("true", "TRUE_STM");
            var ATTRIBUTE = ToTerm ("attribute", "ATTRIBUTE");
            var VARYING = ToTerm ("varying", "VARYING");
            var INOUT = ToTerm ("inout", "INOUT");
            InTerm = ToTerm ("in", "IN");
            OutTerm = ToTerm ("out", "OUT");
            var CONST_STM = ToTerm ("CONST", "CONST");
            var UNIFORM = ToTerm ("uniform", "UNIFORM");
            var CENTROID = ToTerm ("centroid", "CENTROID");
            var SAMPLE = ToTerm ("sample", "SAMPLE");
            var READONLY = ToTerm ("readonly", "READONLY");
            var WRITEONLY = ToTerm ("writeonly", "WRITEONLY");
            var RESTRICT = ToTerm ("restrict", "RESTRICT");
            var VOLATILE = ToTerm ("volatile", "VOLATILE");
            var COHERENT = ToTerm ("coherent", "COHERENT");
            var BUFFER = ToTerm ("buffer", "BUFFER");
            var SHARED = ToTerm ("shared", "SHARED");
            var PATCH = ToTerm ("patch", "PATCH");
            var SUBROUTINE = ToTerm ("subroutine", "SUBROUTINE");
            var LAYOUT = ToTerm ("layout", "LAYOUT");
            var SMOOTH = ToTerm ("smooth", "SMOOTH");
            var FLAT = ToTerm ("flat", "FLAT");
            var NOPERSPECTIVE = ToTerm ("noperspective", "NOPERSPECTIVE");
            var INVARIANT = ToTerm ("invariant", "INVARIANT");
            var PRECISE = ToTerm ("precise", "PRECISE");

            var translation_unit = new NonTerminal("translation_unit"); // CHECKED
            var external_declaration = new NonTerminal("external_declaration"); // CHECKED
            var function_definition = new NonTerminal ("function_definition"); // CHECKED
            var declaration = new NonTerminal ("declaration"); // CHECKED
            var function_prototype = new NonTerminal ("function_prototype"); // CHECKED
            var compound_statement_no_new_scope = new NonTerminal ("compound_statement_no_new_scope"); // CHECKED
            var function_declarator = new NonTerminal ("function_declarator"); // CHECKED
            var statement_list = new NonTerminal("statement_list"); // CHECKED
            var statement = new NonTerminal ("statement"); // CHECKED
            var compound_statement = new NonTerminal ("compound_statement"); // CHECKED
            var simple_statement = new NonTerminal ("simple_statement"); // CHECKED
            var declaration_statement = new NonTerminal ("declaration_statement"); // CHECKED
            var expression_statement = new NonTerminal ("expression_statement"); // CHECKED
            var expression = new NonTerminal ("expression"); // CHECKED
            var assignment_expression = new NonTerminal ("assignment_expression"); // CHECKED
            var conditional_expression = new NonTerminal ("conditional_expression"); // CHECKED
            var logical_or_expression = new NonTerminal ("logical_or_expression"); // CHECKED
            var logical_xor_expression = new NonTerminal ("logical_xor_expression"); // CHECKED
            var logical_and_expression = new NonTerminal ("logical_and_expression"); // CHECKED
            var inclusive_or_expression = new NonTerminal ("inclusive_or_expression"); // CHECKED
            var exclusive_or_expression = new NonTerminal ("exclusive_or_expression"); // CHECKED
            var and_expression = new NonTerminal ("and_expression"); // CHECKED
            var equality_expression = new NonTerminal ("equality_expression"); // CHECKED
            var relational_expression = new NonTerminal ("relational_expression"); // CHECKED
            var shift_expression = new NonTerminal ("shift_expression"); // CHECKED
            var additive_expression = new NonTerminal ("additive_expression"); // CHECKED
            var multiplicative_expression = new NonTerminal ("multiplicative_expression"); // CHECKED
            var unary_expression = new NonTerminal ("unary_expression"); // CHECKED
            var postfix_expression = new NonTerminal ("postfix_expression"); // CHECKED
            var unary_operator = new NonTerminal ("unary_operator"); // CHECKED
            var assignment_operator = new NonTerminal ("assignment_operator"); // CHECKED
            var selection_statement = new NonTerminal ("selection_statement"); // CHECKED
            var selection_rest_statement = new NonTerminal ("selection_rest_statement"); // CHECKED
            var statement_scoped = new NonTerminal ("statement_scoped"); // CHECKED
            var switch_statement = new NonTerminal ("switch_statement"); // CHECKED
            var switch_statement_list = new NonTerminal ("switch_statement_list"); // CHECKED
            var case_label = new NonTerminal ("case_label"); // CHECKED
            var iteration_statement = new NonTerminal ("iteration_statement"); // CHECKED
            var condition = new NonTerminal ("condition"); // CHECKED
            var statement_no_new_scope = new NonTerminal ("statement_no_new_scope"); // CHECKED
            var for_init_statement = new NonTerminal ("for_init_statement"); // CHECKED
            var for_rest_statement = new NonTerminal ("for_rest_statement"); // CHECKED
            var conditionopt = new NonTerminal ("conditionopt"); // CHECKED
            FullySpecifiedType = new NonTerminal ("fully_specified_type"); // CHECKED
            var initializer = new NonTerminal ("initializer"); // CHECKED
            var initializer_list = new NonTerminal ("initializer_list"); // CHECKED
            var type_specifier = new NonTerminal ("type_specifier"); // CHECKED
            var single_type_qualifier = new NonTerminal ("single_type_qualifier"); // CHECKED
            var type_specifier_nonarray = new NonTerminal ("type_specifier_nonarray"); // CHECKED
            TypeQualifier = new NonTerminal ("type_qualifier"); // CHECKED
            var array_specifier = new NonTerminal ("array_specifier"); // CHECKED
            ConstantExpression = new NonTerminal ("constant_expression"); // CHECKED
            StructSpecifier = new NonTerminal ("struct_specifier"); // CHECKED
            var struct_declaration_list = new NonTerminal ("struct_declaration_list"); // CHECKED
            var struct_declaration = new NonTerminal ("struct_declaration"); // CHECKED
            var struct_declarator_list = new NonTerminal ("struct_declarator_list"); // CHECKED
            var struct_declarator = new NonTerminal ("struct_declarator"); // CHECKED
            var jump_statement = new NonTerminal ("jump_statement"); // CHECKED
            var init_declarator_list = new NonTerminal ("init_declarator_list"); // CHECKED
            var precision_qualifier = new NonTerminal ("precision_qualifier"); // CHECKED
            var block_structure = new NonTerminal ("block_structure"); // CHECKED
            SingleDeclaration = new NonTerminal ("single_declaration"); // CHECKED
            var identifier_list = new NonTerminal ("identifier_list"); // CHECKED
            var function_header = new NonTerminal ("function_header"); // CHECKED
            var function_header_with_parameters = new NonTerminal ("function_header_with_parameters"); // CHECKED
            var parameter_declaration = new NonTerminal ("parameter_declaration"); // CHECKED
            var parameter_declarator = new NonTerminal ("parameter_declarator"); // CHECKED
            var parameter_type_specifier = new NonTerminal ("parameter_type_specifier"); // CHECKED
            PrimaryExpression = new NonTerminal ("primary_expression"); // CHECKED
            var integer_expression = new NonTerminal ("integer_expression"); // CHECKED
            var function_call = new NonTerminal ("function_call"); // CHECKED
            var function_call_or_method = new NonTerminal ("function_call_or_method"); // CHECKED
            var function_call_generic = new NonTerminal ("function_call_generic"); // CHECKED
            var function_call_header_no_parameters = new NonTerminal ("function_call_header_no_parameters"); // CHECKED
            var function_call_header_with_parameters = new NonTerminal ("function_call_header_with_parameters"); // CHECKED
            var function_call_header = new NonTerminal ("function_call_header"); // CHECKED
            var function_identifier = new NonTerminal ("function_identifier"); // CHECKED
            var variable_identifier = new NonTerminal ("variable_identifier"); // CHECKED
            var boolconstant = new NonTerminal ("boolconstant"); // CHECKED
            var storage_qualifier = new NonTerminal ("storage_qualifier"); // CHECKED
            var type_name_list = new NonTerminal ("type_name_list"); // CHECKED
            LayoutQualifier = new NonTerminal ("layout_qualifier"); // CHECKED
            LayoutQualifierIdList = new NonTerminal ("layout_qualifier_id_list"); // CHECKED
            LayoutQualifierId = new NonTerminal ("layout_qualifier_id"); // CHECKED
            var interpolation_qualifier = new NonTerminal ("interpolation_qualifier"); // CHECKED
            var invariant_qualifier = new NonTerminal ("invariant_qualifier"); // CHECKED
            var precise_qualifier = new NonTerminal ("precise_qualifier"); // CHECKED

            var array_empty_bracket = new NonTerminal ("array_empty_bracket");
            var constant_inside_bracket = new NonTerminal ("constant_inside_bracket");
            var floating_number_value = new NonTerminal ("floating_number_value");

            // Place Rules Here
            this.Root = translation_unit;

            //translation_unit.Rule = external_declaration | translation_unit + external_declaration;
            translation_unit.Rule = MakePlusRule(translation_unit, external_declaration);

            external_declaration.Rule =  function_definition | declaration;

            declaration.Rule = function_prototype + SEMICOLON
                | init_declarator_list + SEMICOLON
                | PRECISION + precision_qualifier + type_specifier + SEMICOLON
                | block_structure + SEMICOLON
                | block_structure + IDENTIFIER + SEMICOLON
                | block_structure + IDENTIFIER + array_specifier + SEMICOLON
                | TypeQualifier + SEMICOLON
                | TypeQualifier + IDENTIFIER + SEMICOLON
                | TypeQualifier + IDENTIFIER + identifier_list + SEMICOLON;

            identifier_list.Rule = COMMA + IDENTIFIER
                | identifier_list + COMMA + IDENTIFIER;

            //			identifier_list.Rule = MakePlusRule (identifier_list, COMMA, IDENTIFIER);

            precision_qualifier.Rule = HIGH_PRECISION
                            | MEDIUM_PRECISION
                            | LOW_PRECISION;

            block_structure.Rule = TypeQualifier + IDENTIFIER + LEFT_BRACE + struct_declaration_list + RIGHT_BRACE;

            init_declarator_list.Rule = SingleDeclaration
                | init_declarator_list + COMMA + IDENTIFIER
                | init_declarator_list + COMMA + IDENTIFIER + array_specifier
                | init_declarator_list + COMMA + IDENTIFIER + array_specifier + EQUAL + initializer
                | init_declarator_list + COMMA + IDENTIFIER + EQUAL + initializer;

            SingleDeclaration.Rule = FullySpecifiedType
                | FullySpecifiedType + IDENTIFIER
                | FullySpecifiedType + IDENTIFIER + array_specifier
                | FullySpecifiedType + IDENTIFIER + array_specifier + EQUAL + initializer
                | FullySpecifiedType + IDENTIFIER + EQUAL + initializer;

            function_definition.Rule = function_prototype + compound_statement_no_new_scope;

            function_prototype.Rule = function_declarator + RIGHT_PAREN;

            function_declarator.Rule = function_header
                | function_header_with_parameters;

            function_header.Rule = FullySpecifiedType + IDENTIFIER + LEFT_PAREN;

            function_header_with_parameters.Rule = function_header + parameter_declaration
                | function_header_with_parameters + COMMA + parameter_declaration;

            //			function_header_with_parameters.Rule = MakePlusRule (function_header_with_parameters, COMMA, parameter_declaration);

            parameter_declaration.Rule = TypeQualifier + parameter_declarator
                | parameter_declarator
                | TypeQualifier + parameter_type_specifier
                | parameter_type_specifier;

            parameter_declarator.Rule = type_specifier + IDENTIFIER
                | type_specifier + IDENTIFIER + array_specifier;

            parameter_type_specifier.Rule = type_specifier;

            compound_statement_no_new_scope.Rule = LEFT_BRACE + RIGHT_BRACE | LEFT_BRACE + statement_list + RIGHT_BRACE;

            statement_list.Rule = MakeStarRule (statement_list, statement);
            //				statement | statement_list + statement;
            //			statement_list.Rule = MakePlusRule(statement_list, statement);

            statement.Rule = compound_statement | simple_statement;

            compound_statement.Rule = LEFT_BRACE + RIGHT_BRACE | LEFT_BRACE + statement_list + RIGHT_BRACE;

            simple_statement.Rule = declaration_statement
                                        | expression_statement
                                        | selection_statement
                                        | switch_statement
                                        | case_label
                                        | iteration_statement
                                        | jump_statement;

            declaration_statement.Rule = declaration;

            expression_statement.Rule = SEMICOLON | expression + SEMICOLON;

            expression.Rule = assignment_expression | expression  + COMMA + assignment_expression;
            //expression.Rule = MakePlusRule(expression, COMMA, assignment_expression);

            assignment_expression.Rule = conditional_expression	| unary_expression  + assignment_operator + assignment_expression;

            conditional_expression.Rule = logical_or_expression | logical_or_expression  + QUESTION + expression + COLON + assignment_expression;

            logical_or_expression.Rule = logical_xor_expression | logical_or_expression + OR_OP + logical_xor_expression;

            logical_xor_expression.Rule = logical_and_expression | logical_xor_expression + XOR_OP + logical_and_expression;

            logical_and_expression.Rule = inclusive_or_expression | logical_and_expression + AND_OP + inclusive_or_expression;

            inclusive_or_expression.Rule = exclusive_or_expression | inclusive_or_expression + VERTICAL_BAR + exclusive_or_expression;

            exclusive_or_expression.Rule = and_expression | exclusive_or_expression + CARET + and_expression;

            and_expression.Rule = equality_expression | and_expression + AMPERSAND +  equality_expression;
            //and_expression = MakePlusRule(and_expression, AMPERSAND, equality_expression);

            equality_expression.Rule = relational_expression
                        | equality_expression + EQ_OP + relational_expression
                        | equality_expression + NE_OP + relational_expression;

            relational_expression.Rule = shift_expression
                | relational_expression + LEFT_ANGLE + shift_expression
                | relational_expression + RIGHT_ANGLE + shift_expression
                | relational_expression + LE_OP + shift_expression
                | relational_expression + GE_OP + shift_expression;

            shift_expression.Rule =  additive_expression
                    | shift_expression + LEFT_OP + additive_expression
                        | shift_expression + RIGHT_OP + additive_expression;

            //	shift_expression.Rule = additive_expression
            //		| MakePlusRule (shift_expression, LEFT_OP, additive_expression)
            //			| MakePlusRule (shift_expression, RIGHT_OP, additive_expression);

            additive_expression.Rule = multiplicative_expression
                        | additive_expression + PLUS + multiplicative_expression
                        | additive_expression + DASH  + multiplicative_expression;

            multiplicative_expression.Rule = unary_expression
                | multiplicative_expression + STAR + unary_expression
                | multiplicative_expression + SLASH + unary_expression
                | multiplicative_expression + PERCENT + unary_expression;

            unary_expression.Rule =
                 floating_number_value
                | postfix_expression
                | INC_OP + unary_expression
                | DEC_OP + unary_expression
                | unary_operator + unary_expression;

            floating_number_value.Rule = INTCONSTANT + DOT + REMAINDER;

            postfix_expression.Rule = ImplyPrecedenceHere(1) + PrimaryExpression
                | postfix_expression + LEFT_BRACKET + integer_expression + RIGHT_BRACKET
                | ImplyPrecedenceHere(4) + FLOATCONSTANT
                | ImplyPrecedenceHere(3) + postfix_expression + DOT + INTCONSTANT // for floating values
                | ImplyPrecedenceHere(2) +  postfix_expression + DOT + FIELD_SELECTION
                | postfix_expression + INC_OP
                | postfix_expression + DEC_OP;

            integer_expression.Rule = expression;

            function_call.Rule = function_call_or_method;

            function_call_or_method.Rule = function_call_generic;

            function_call_generic.Rule = function_call_header_with_parameters + RIGHT_PAREN
                | function_call_header_no_parameters + RIGHT_PAREN;

            //			function_call_header_with_parameters.Rule = function_call_header + assignment_expression
            //				| function_call_header_with_parameters + COMMA + assignment_expression;
            function_call_header_with_parameters.Rule = function_call_header + assignment_expression
                | MakePlusRule(function_call_header_with_parameters, COMMA, assignment_expression);

            function_call_header.Rule = function_identifier + LEFT_PAREN;

            function_identifier.Rule = type_specifier
                | postfix_expression;

            function_call_header_no_parameters.Rule = function_call_header + VOID_STM
                | function_call_header;

            PrimaryExpression.Rule = variable_identifier
                | INTCONSTANT
                | UINTCONSTANT
                | FLOATCONSTANT
                | DOUBLECONSTANT
                | boolconstant
                | LEFT_PAREN + expression + RIGHT_PAREN;

            boolconstant.Rule = TRUE_STM | FALSE_STM;

            variable_identifier.Rule = IDENTIFIER;

            unary_operator.Rule = PLUS
                    | DASH
                    | BANG
                    | TILDE;

            assignment_operator.Rule = EQUAL
                    | MUL_ASSIGN
                    | DIV_ASSIGN
                    | MOD_ASSIGN
                    | ADD_ASSIGN
                    | SUB_ASSIGN
                    | LEFT_ASSIGN
                    | RIGHT_ASSIGN
                    | AND_ASSIGN
                    | XOR_ASSIGN
                    | OR_ASSIGN;

            selection_statement.Rule = IF_STM + LEFT_PAREN + expression + RIGHT_PAREN  + selection_rest_statement;

            selection_rest_statement.Rule = statement_scoped + ELSE + statement_scoped
                        | statement_scoped;

            statement_scoped.Rule =  compound_statement	| simple_statement;

            switch_statement.Rule = SWITCH_STM  + LEFT_PAREN + expression + RIGHT_PAREN + LEFT_BRACE + switch_statement_list + RIGHT_BRACE;

            switch_statement_list.Rule = Empty | statement_list;

            case_label.Rule = CASE_STM + expression + COLON
                | DEFAULT_STM + COLON;

            iteration_statement.Rule = WHILE + LEFT_PAREN + condition + RIGHT_PAREN + statement_no_new_scope
                | DO_STM + statement + WHILE + LEFT_PAREN + expression + RIGHT_PAREN + SEMICOLON
                | FOR_STM +  LEFT_PAREN + for_init_statement + for_rest_statement + RIGHT_PAREN + statement_no_new_scope;

            condition.Rule = expression
                    | FullySpecifiedType + IDENTIFIER + EQUAL + initializer;

            statement_no_new_scope.Rule = compound_statement_no_new_scope
                        | simple_statement;

            for_init_statement.Rule = expression_statement
                | declaration_statement;

            for_rest_statement.Rule = conditionopt  + SEMICOLON
                    | conditionopt + SEMICOLON + expression;

            conditionopt.Rule = condition
                    | Empty;

            initializer.Rule = assignment_expression
                | LEFT_BRACE + initializer_list + RIGHT_BRACE
                | LEFT_BRACE + initializer_list + COMMA + RIGHT_BRACE;

            initializer_list.Rule =  initializer
                | initializer_list + COMMA + initializer;

            FullySpecifiedType.Rule = type_specifier
                | TypeQualifier + type_specifier;

            type_specifier.Rule = type_specifier_nonarray
                | type_specifier_nonarray + array_specifier;

            //			array_specifier.Rule = LEFT_BRACKET + RIGHT_BRACKET
            //				| LEFT_BRACKET + constant_expression + RIGHT_BRACKET
            //				| array_specifier + LEFT_BRACKET + RIGHT_BRACKET
            //				| array_specifier + LEFT_BRACKET + constant_expression + RIGHT_BRACKET;

            array_specifier.Rule =
                  MakePlusRule (array_specifier, array_empty_bracket)
                | MakePlusRule (array_specifier, constant_inside_bracket);

            // two additional rules
            array_empty_bracket.Rule = LEFT_BRACKET + RIGHT_BRACKET;
            constant_inside_bracket.Rule = LEFT_BRACKET + ConstantExpression + RIGHT_BRACKET;

            ConstantExpression.Rule = conditional_expression;

            //			type_qualifier.Rule = single_type_qualifier
            //				| type_qualifier + single_type_qualifier;

            TypeQualifier.Rule = MakePlusRule(TypeQualifier, single_type_qualifier);

            single_type_qualifier.Rule = storage_qualifier
                    | LayoutQualifier
                    | precision_qualifier
                    | interpolation_qualifier
                    | invariant_qualifier
                    | precise_qualifier;

            LayoutQualifier.Rule = LAYOUT + LEFT_PAREN + LayoutQualifierIdList + RIGHT_PAREN;

            //			layout_qualifier_id_list.Rule =  layout_qualifier_id
            //				| layout_qualifier_id_list + COMMA + layout_qualifier_id;

            LayoutQualifierIdList.Rule = MakePlusRule (LayoutQualifierIdList, COMMA, LayoutQualifierId);

            LayoutQualifierId.Rule = IDENTIFIER
                    | IDENTIFIER + EQUAL  + ConstantExpression
                    | SHARED;

            storage_qualifier.Rule = CONST_STM
                | ATTRIBUTE
                | VARYING
                | INOUT
                | InTerm
                | OutTerm
                | CENTROID
                | PATCH
                | SAMPLE
                | UNIFORM
                | BUFFER
                | SHARED
                | COHERENT
                | VOLATILE
                | RESTRICT
                | READONLY
                | WRITEONLY
                | SUBROUTINE
                | SUBROUTINE + LEFT_PAREN + type_name_list + RIGHT_PAREN;

            interpolation_qualifier.Rule = SMOOTH
                | FLAT
                | NOPERSPECTIVE;

            invariant_qualifier.Rule = INVARIANT;

            precise_qualifier.Rule = PRECISE;

            type_name_list.Rule = TYPE_NAME
                    | type_name_list + COMMA + TYPE_NAME;

            type_specifier_nonarray.Rule = VOID_STM
                | FLOAT_TKN
                | DOUBLE_TKN
                | INT_TKN
                | UINT_TKN
                | BOOL_TKN
                | VEC2
                | VEC3
                | VEC4
                | DVEC2
                | DVEC3
                | DVEC4
                | BVEC2
                | BVEC3
                | BVEC4
                | IVEC2
                | IVEC3
                | IVEC4
                | UVEC2
                | UVEC3
                | UVEC4
                | MAT2
                | MAT3
                | MAT4
                | MAT2X2
                | MAT2X3
                | MAT2X4
                | MAT3X2
                | MAT3X3
                | MAT3X4
                | MAT4X2
                | MAT4X3
                | MAT4X4
                | DMAT2
                | DMAT3
                | DMAT4
                | DMAT2X2
                | DMAT2X3
                | DMAT2X4
                | DMAT3X2
                | DMAT3X3
                | DMAT3X4
                | DMAT4X2
                | DMAT4X3
                | DMAT4X4
                | ATOMIC_UINT
                | SAMPLER1D
                | SAMPLER2D
                | SAMPLER3D
                | SAMPLERCUBE
                | SAMPLER1DSHADOW
                | SAMPLER2DSHADOW
                | SAMPLERCUBESHADOW
                | SAMPLER1DARRAY
                | SAMPLER2DARRAY
                | SAMPLER1DARRAYSHADOW
                | SAMPLER2DARRAYSHADOW
                | SAMPLERCUBEARRAY
                | SAMPLERCUBEARRAYSHADOW
                | ISAMPLER1D
                | ISAMPLER2D
                | ISAMPLER3D
                | ISAMPLERCUBE
                | ISAMPLER1DARRAY
                | ISAMPLER2DARRAY
                | ISAMPLERCUBEARRAY
                | USAMPLER1D
                | USAMPLER2D
                | USAMPLER3D
                | USAMPLERCUBE
                | USAMPLER1DARRAY
                | USAMPLER2DARRAY
                | USAMPLERCUBEARRAY
                | SAMPLER2DRECT
                | SAMPLER2DRECTSHADOW
                | ISAMPLER2DRECT
                | USAMPLER2DRECT
                | SAMPLERBUFFER
                | ISAMPLERBUFFER
                | USAMPLERBUFFER
                | SAMPLER2DMS
                | ISAMPLER2DMS
                | USAMPLER2DMS
                | SAMPLER2DMSARRAY
                | ISAMPLER2DMSARRAY
                | USAMPLER2DMSARRAY
                | IMAGE1D
                | IIMAGE1D
                | UIMAGE1D
                | IMAGE2D
                | IIMAGE2D
                | UIMAGE2D
                | IMAGE3D
                | IIMAGE3D
                | UIMAGE3D
                | IMAGE2DRECT
                | IIMAGE2DRECT
                | UIMAGE2DRECT
                | IMAGECUBE
                | IIMAGECUBE
                | UIMAGECUBE
                | IMAGEBUFFER
                | IIMAGEBUFFER
                | UIMAGEBUFFER
                | IMAGE1DARRAY
                | IIMAGE1DARRAY
                | UIMAGE1DARRAY
                | IMAGE2DARRAY
                | IIMAGE2DARRAY
                | UIMAGE2DARRAY
                | IMAGECUBEARRAY
                | IIMAGECUBEARRAY
                | UIMAGECUBEARRAY
                | IMAGE2DMS
                | IIMAGE2DMS
                | UIMAGE2DMS
                | IMAGE2DMSARRAY
                | IIMAGE2DMSARRAY
                | UIMAGE2DMSARRAY
                | SAMPLEREXTERNALOES
                | StructSpecifier
                | TYPE_NAME;

            StructSpecifier.Rule = STRUCT_STM + IDENTIFIER + LEFT_BRACE + struct_declaration_list + RIGHT_BRACE
                | STRUCT_STM + LEFT_BRACE + struct_declaration_list + RIGHT_BRACE;

            //			struct_declaration_list.Rule = struct_declaration
            //				| struct_declaration_list + struct_declaration;

            struct_declaration_list.Rule = MakePlusRule (struct_declaration_list, struct_declaration);

            struct_declaration.Rule = type_specifier + struct_declarator_list + SEMICOLON
                | TypeQualifier + type_specifier + struct_declarator_list  + SEMICOLON;

            //			struct_declarator_list.Rule = struct_declarator
            //				| struct_declarator_list + COMMA + struct_declarator;

            struct_declarator_list.Rule = MakePlusRule (struct_declarator_list, COMMA, struct_declarator);

            struct_declarator.Rule = IDENTIFIER
                | IDENTIFIER + array_specifier;

            jump_statement.Rule = CONTINUE + SEMICOLON
                | BREAK_STM + SEMICOLON
                | RETURN_STM + SEMICOLON
                | RETURN_STM + expression + SEMICOLON
                | DISCARD + SEMICOLON;

            this.MarkPunctuation (LEFT_BRACKET, RIGHT_BRACKET, LEFT_BRACE, RIGHT_BRACE, LEFT_PAREN, RIGHT_PAREN, SEMICOLON);

            this.MarkTransient(
            //				translation_unit,
            //				external_declaration,
            //				//function_definition,
            //				//declaration,
            //				//function_prototype,
            //				compound_statement_no_new_scope,
            //				function_declarator,
            //				statement_list,
            //				statement,
            //				compound_statement,
            //				simple_statement,
            //				//declaration_statement,
            //				expression_statement,
            //				expression,
            //				assignment_expression,
                conditional_expression,
                logical_or_expression,
                logical_xor_expression,
                logical_and_expression,
                inclusive_or_expression,
                exclusive_or_expression,
                and_expression,
                equality_expression,
                relational_expression,
                shift_expression,
                additive_expression,
                multiplicative_expression,
                unary_expression,
            //				postfix_expression,
                unary_operator,
            //				assignment_operator,
            //				selection_statement,
            //				selection_rest_statement,
            //				statement_scoped,
            //				switch_statement,
            //				switch_statement_list,
            //				case_label,
            //				iteration_statement,
            //				//condition,
            //				statement_no_new_scope,
            //				for_init_statement,
            //				for_rest_statement,
            //				conditionopt,
            //				fully_specified_type,
            //				initializer,
            //				initializer_list,
                type_specifier,
                single_type_qualifier,
                type_specifier_nonarray,
            //				//type_qualifier,
            //				//array_specifier,
            //				constant_expression,
            //				struct_specifier,
            //				struct_declaration_list,
            //				//struct_declaration,
                struct_declarator_list,
            //				struct_declarator,
            //				jump_statement,
            //				init_declarator_list,
            //				precision_qualifier,
            //				block_structure,
            //				single_declaration,
            //				identifier_list,
            //				//function_header,
            //				function_header_with_parameters,
            //				parameter_declaration,
            //				parameter_declarator,
            //				parameter_type_specifier,
                PrimaryExpression,
            //				integer_expression,
            ////				function_call,
            ////				function_call_generic,
            //				function_call_header_with_parameters,
            //				function_call_header_no_parameters
            ////				function_call_header,
            ////				function_identifier,
            //				//variable_identifier,
            //				boolconstant,
            //				type_name_list,
                storage_qualifier,
            //				layout_qualifier_id,
            //				invariant_qualifier
            //				array_empty_bracket
                constant_inside_bracket,
                //floating_number_value,
                DOT
            );
        }
Esempio n. 8
0
 // About exponent symbols, extract 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.
 // ...
 // In addition, the exponent marker e specifies the default precision for the implementation. The default precision 
 //  has at least as much precision as double, but implementations may wish to allow this default to be set by the user.
 public static NumberLiteral CreateSchemeNumber(string name) {
   NumberLiteral term = new NumberLiteral(name);
   term.DefaultIntTypes = new TypeCode[] { TypeCode.Int32, TypeCode.Int64, NumberLiteral.TypeCodeBigInt };
   term.DefaultFloatType = TypeCode.Double; // it is default
   term.AddExponentSymbols("eE", TypeCode.Double); //default precision for platform, double 
   term.AddExponentSymbols("sSfF", TypeCode.Single); 
   term.AddExponentSymbols("dDlL", TypeCode.Double); 
   term.AddPrefix("#b", NumberOptions.Binary);
   term.AddPrefix("#o", NumberOptions.Octal);
   term.AddPrefix("#x", NumberOptions.Hex);
   term.AddPrefix("#d", NumberOptions.None);
   term.AddPrefix("#i", NumberOptions.None); // inexact prefix, has no effect
   term.AddPrefix("#e", NumberOptions.None); // exact prefix, has no effect
   term.AddSuffix("J", NumberLiteral.TypeCodeImaginary);
   return term;
 }
Esempio n. 9
0
 //http://docs.python.org/ref/numbers.html
 public static NumberLiteral CreatePythonNumber(string name) {
   NumberLiteral term = new NumberLiteral(name, NumberOptions.AllowStartEndDot);
   //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.AddPrefix("0x", NumberOptions.Hex);
   term.AddPrefix("0", NumberOptions.Octal);
   term.AddSuffix("L", TypeCode.Int64, NumberLiteral.TypeCodeBigInt);
   term.AddSuffix("J", NumberLiteral.TypeCodeImaginary);
   return term;
 }
Esempio n. 10
0
        // BASIC is not case sensitive...
        public GWBasicGrammar()
            : base(false)
        {
            this.GrammarComments = "This grammar uses one new Irony feature - Scanner-Parser link. Parser helps Scanner to disambiguate " +
                             "similar but different token types when more than one terminal matches the current input char.\r\n" +
                             "See comments in GwBasicGrammar.cs file.";
              /*
               Scanner-Parser link.
               The grammar defines 3 terminals for numbers: number, lineNumber and fileNumber. All three return decimal
               digits in GetFirsts() method. So when current input is a digit, the scanner has 3 potential candidate terminals
               to match the input. However, because each of the 3 terminals can appear in specific "places" in grammar,
               the parser is able to assist scanner to pick the correct terminal, depending on the current parser state.
               The disambiguation happens in Scanner.SelectTerminals method. When the terminal list for current input char
               has more than 1 terminal, the scanner code gets the current parser state from core parser (through compilerContext),
               and then checks the terminals agains the ExpectedTerms set in the parser state.
               As you might see in Grammar Explorer, the conflict is resolved successfully.
               */

            //Terminals
            var lineNumber = new NumberLiteral("LINE_NUMBER", NumberOptions.IntOnly);
            var fileNumber = new NumberLiteral("FILE_NUMBER", NumberOptions.IntOnly);

            var number = new NumberLiteral("NUMBER", NumberOptions.AllowStartEndDot);
            //ints that are too long for int32 are converted to int64
            number.DefaultIntTypes = new TypeCode[] {TypeCode.Int32, TypeCode.Int64};
            number.AddExponentSymbols("eE", TypeCode.Single);
            number.AddExponentSymbols("dD", TypeCode.Double);
            number.AddSuffix("!", TypeCode.Single);
            number.AddSuffix("#", TypeCode.Double);

            var variable = new IdentifierTerminal("Identifier");
            variable.AddSuffix("$", TypeCode.String);
            variable.AddSuffix("%", TypeCode.Int32);
            variable.AddSuffix("!", TypeCode.Single);
            variable.AddSuffix("#", TypeCode.Double);

            var stringLiteral = new StringLiteral("STRING", "\"", StringOptions.None);
            //Important: do not add comment term to base.NonGrammarTerminals list - we do use this terminal in grammar rules
            var userFunctionName = variable;
            var comment = new CommentTerminal("Comment", "REM", "\n");
            var short_comment = new CommentTerminal("ShortComment", "'", "\n");
            var line_cont = new LineContinuationTerminal("LineContinuation", "_");
            var comma = ToTerm(",", "comma");
            var colon = ToTerm(":", "colon");

            var comma_opt = new NonTerminal("comma_opt");
            comma_opt.Rule = Empty | ",";
            var semi_opt = new NonTerminal("semi_opt");
            semi_opt.Rule = Empty | ";";
            var pound_opt = new NonTerminal("pound_opt");
            pound_opt.Rule = Empty | "#";

            // Non-terminals
            var PROGRAM = new NonTerminal("PROGRAM");
            var LINE = new NonTerminal("LINE");
            var LINE_CONTENT_OPT = new NonTerminal("LINE_CONTENT_OPT");
            var COMMENT_OPT = new NonTerminal("COMMENT_OPT");
            var STATEMENT_LIST = new NonTerminal("STATEMENT_LIST");
            var STATEMENT = new NonTerminal("STATEMENT");
            var PRINT_STMT = new NonTerminal("PRINT_STMT");
            var PRINT_LIST = new NonTerminal("PRINT_LIST");
            var PRINT_ARG = new NonTerminal("PRINT_ARG");
            var OPEN_STMT = new NonTerminal("OPEN_STMT");
            var OPEN_STMT_MODE = new NonTerminal("OPEN_STMT_MODE");
            var OPEN_STMT_ACCESS = new NonTerminal("OPEN_STMT_ACCESS");
            var CLOSE_STMT = new NonTerminal("CLOSE_STMT");
            var INPUT_STMT = new NonTerminal("INPUT_STMT");
            var VARIABLES = new NonTerminal("VARIABLES");
            var IF_STMT = new NonTerminal("IF_STMT");
            var THEN_CLAUSE = new NonTerminal("THEN_CLAUSE");
            var ELSE_CLAUSE_OPT = new NonTerminal("ELSE_CLAUSE_OPT"); //, typeof(AstNode));
            var EXPR = new NonTerminal("EXPRESSION");
            var EXPR_LIST = new NonTerminal("EXPRESSION_LIST");
            var BINARY_OP = new NonTerminal("BINARY_OP", "operator");
            var BINARY_EXPR = new NonTerminal("BINARY_EXPR");
            var UNARY_EXPR = new NonTerminal("UNARY_EXPR");
            var SIGN = new NonTerminal("SIGN");
            var ASSIGN_STMT = new NonTerminal("ASSIGN_STMT");
            var FOR_STMT = new NonTerminal("FOR_STMT");
            var STEP_OPT = new NonTerminal("STEP_OPT");
            var NEXT_STMT = new NonTerminal("NEXT_STMT");
            var LOCATE_STMT = new NonTerminal("LOCATE_STMT");
            var WHILE_STMT = new NonTerminal("WHILE_STMT");
            var WEND_STMT = new NonTerminal("WEND_STMT");
            var SWAP_STMT = new NonTerminal("SWAP_STMT");
            var FUN_CALL = new NonTerminal("FUN_CALL");
            var VARIABLE_OR_FUNCTION_EXPR = new NonTerminal("VARIABLE_OR_FUNCTION_EXPR");
            var ARG_LIST = new NonTerminal("ARG_LIST");
            var LINE_INPUT_STMT = new NonTerminal("LINE_INPUT_STMT");
            var LINE_INPUT_POUND_STMT = new NonTerminal("LINE_INPUT_POUND_STMT");
            var END_STMT = new NonTerminal("END_STMT");
            var CLS_STMT = new NonTerminal("CLS_STMT");
            var CLEAR_STMT = new NonTerminal("CLEAR_STMT");
            var DIM_STMT = new NonTerminal("DIM_STMT");
            var DEF_FN_STMT = new NonTerminal("DEF_FN_STMT");
            var GOTO_STMT = new NonTerminal("GOTO_STMT");
            var GOSUB_STMT = new NonTerminal("GOSUB_STMT");
            var RETURN_STMT = new NonTerminal("RETURN_STMT");
            var ON_STMT = new NonTerminal("ON_STMT");
            var LINE_NUMBERS = new NonTerminal("LINE_NUMBERS");
            var RANDOMIZE_STMT = new NonTerminal("RANDOMIZE_STMT");

            // set the PROGRAM to be the root node of BASIC programs.
            this.Root = PROGRAM;

            // BNF Rules
            PROGRAM.Rule = MakePlusRule(PROGRAM, 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 | lineNumber + LINE_CONTENT_OPT + COMMENT_OPT + NewLine | SyntaxError + NewLine;
            LINE.NodeCaptionTemplate = "Line #{0}";

            // A statement list is 1 or more statements separated by the ':' character
            LINE_CONTENT_OPT.Rule = Empty | IF_STMT | STATEMENT_LIST;
            STATEMENT_LIST.Rule = MakePlusRule(STATEMENT_LIST, colon, STATEMENT);
            COMMENT_OPT.Rule = short_comment | comment | Empty;

            // A statement can be one of a number of types
            STATEMENT.Rule = ASSIGN_STMT | PRINT_STMT | INPUT_STMT | OPEN_STMT | CLOSE_STMT
                | LINE_INPUT_POUND_STMT | LINE_INPUT_STMT
                | LOCATE_STMT | CLS_STMT
                | END_STMT | CLEAR_STMT | DIM_STMT | DEF_FN_STMT
                | SWAP_STMT | RANDOMIZE_STMT
                | GOSUB_STMT | RETURN_STMT | GOTO_STMT | ON_STMT
                | FOR_STMT | NEXT_STMT | WHILE_STMT | WEND_STMT;

            // The different statements are defined here
            PRINT_STMT.Rule = "print" + PRINT_LIST;
            PRINT_LIST.Rule = MakeStarRule(PRINT_LIST, null, PRINT_ARG);
            PRINT_ARG.Rule = EXPR + semi_opt;
            INPUT_STMT.Rule = "input" + semi_opt + stringLiteral + ";" + VARIABLES;
            OPEN_STMT.Rule = "open" + EXPR + (Empty | "for" + OPEN_STMT_MODE) +
                (Empty | "access" + OPEN_STMT_ACCESS) + "as" + pound_opt + fileNumber;
            OPEN_STMT_ACCESS.Rule = "read" + (Empty | "write") | "write";
            OPEN_STMT_MODE.Rule = ToTerm("o") | "i" | "a" | "output" | "input" | "append";
            CLOSE_STMT.Rule = "close" + pound_opt + fileNumber;
            LINE_INPUT_STMT.Rule = ToTerm("line") + "input" + semi_opt + stringLiteral + ";" + VARIABLE_OR_FUNCTION_EXPR;
            LINE_INPUT_POUND_STMT.Rule = ToTerm("line") + "input" + ToTerm("#") + fileNumber + comma + VARIABLE_OR_FUNCTION_EXPR;
            DIM_STMT.Rule = "dim" + VARIABLES;
            DEF_FN_STMT.Rule = "def" + userFunctionName + (Empty | "(" + ARG_LIST + ")") + "=" + EXPR;
            VARIABLES.Rule = VARIABLE_OR_FUNCTION_EXPR | VARIABLE_OR_FUNCTION_EXPR + "," + VARIABLES;

            IF_STMT.Rule = "if" + EXPR + THEN_CLAUSE + ELSE_CLAUSE_OPT;
            THEN_CLAUSE.Rule = "then" + STATEMENT_LIST | GOTO_STMT;

            //Inject PreferShift hint here to explicitly set shift as preferred action. Suppresses warning message about conflict.
            ELSE_CLAUSE_OPT.Rule = Empty | PreferShiftHere()  + "else" + STATEMENT_LIST;

            GOTO_STMT.Rule = "goto" + lineNumber;
            GOSUB_STMT.Rule = "gosub" + lineNumber;
            RETURN_STMT.Rule = "return";
            ON_STMT.Rule = "on" + EXPR + (ToTerm("goto") | "gosub") + LINE_NUMBERS;
            LINE_NUMBERS.Rule = MakePlusRule(LINE_NUMBERS, comma, lineNumber);
            ASSIGN_STMT.Rule = VARIABLE_OR_FUNCTION_EXPR + "=" + EXPR;
            LOCATE_STMT.Rule = "locate" + EXPR + comma + EXPR;
            SWAP_STMT.Rule = "swap" + EXPR + comma + EXPR;
            END_STMT.Rule = "end";
            CLS_STMT.Rule = "cls";
            CLEAR_STMT.Rule = ToTerm("clear") + comma + (Empty | number) + (Empty | comma + number) | "clear" + number | "clear";
            RANDOMIZE_STMT.Rule = "randomize" + EXPR;

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

            //Inject PreferShift hint here to explicitly set shift as preferred action. Suppresses warning message about conflict.
            //The conflict arises from PRINT statement, when there may be ambigous interpretations for expression like
            //  PRINT F (X) -- either function call or identifier followed by expression
            FUN_CALL.Rule = variable + PreferShiftHere() + "(" + ARG_LIST + ")";
            VARIABLE_OR_FUNCTION_EXPR.Rule = variable | FUN_CALL;

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

            EXPR_LIST.Rule = MakeStarRule(EXPR_LIST, EXPR);

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

            //TODO: check number of arguments for particular function in node constructor
            ARG_LIST.Rule = MakePlusRule(ARG_LIST, comma, EXPR);

            //Punctuation and Transient elements
            MarkPunctuation("(", ")", ",");
            MarkTransient(EXPR, STATEMENT, LINE_CONTENT_OPT, VARIABLE_OR_FUNCTION_EXPR, COMMENT_OPT);
            NonGrammarTerminals.Add(line_cont);

            this.LanguageFlags = LanguageFlags.NewLineBeforeEOF;

            lineNumber.ValidateToken += lineNumber_ValidateToken;
        }
Esempio n. 11
0
        public ClarionGrammar() : base(false) //case insensitive
        {
            base.GrammarComments = "Clarion grammar for parsing Clarion sources.";

            //Terminals
            var comment = new CommentTerminal("comment", "!", "\n");

            this.NonGrammarTerminals.Add(comment);
            _comment = comment;
            var identifier = new IdentifierTerminal("identifier", ":", string.Empty);
            var label      = new IdentifierTerminal("label", ":", string.Empty);

            label.ValidateToken += label_ValidateToken;
            label.Priority       = TerminalPriority.High;
            //Clarion uses suffixes for identifying number base (bin, octal, hex) - unlike Irony's NumberLiteral which supports suffixes.
            // We treat all numbers as hexadecimal with optional sufffixes that don't mean anything. We "fix" the value in ValidateToken handler
            var number = new NumberLiteral("number");

            number.Options = NumberOptions.Hex | NumberOptions.DisableQuickParse;
            number.AddSuffix("b", TypeCode.Int64);
            number.AddSuffix("o", TypeCode.Int64);
            number.AddSuffix("h", TypeCode.Int64);
            number.ValidateToken += number_ValidateToken;

            var string_lit = new StringLiteral("string_lit", "'", StringOptions.AllowsDoubledQuote | StringOptions.NoEscapes);

            var comma = ToTerm(",");
            var not   = ToTerm("NOT");

            //Non-terminals
            var source_file = new NonTerminal("source_file");
            var main_file   = new NonTerminal("main_file");
            //whitespace - NewLine, Empty line, line with compiler directive, or line with end-tag of Compile(..) directive
            var wspace               = new NonTerminal("wspace");
            var ws_lines             = new NonTerminal("ws_lines");
            var ws_line              = new NonTerminal("ws_line");
            var compiler_dir         = new NonTerminal("compiler_dir");
            var compiler_dir_line    = new NonTerminal("compiler_dir_line");
            var cdir_compile_end_tag = new CustomTerminal("compile_end_tag", Match_compile_end_tag);

            var cdir_include         = new NonTerminal("cdir_include");
            var cdir_equate          = new NonTerminal("cdir_equate");
            var cdir_compile_omit    = new NonTerminal("cdir_compile_omit");
            var compile_or_omit      = new NonTerminal("compile_or_omit");
            var cdir_section         = new NonTerminal("cdir_section");
            var once_opt             = new NonTerminal("once_opt");
            var const_value          = new NonTerminal("const_value");
            var comma_expr_opt       = new NonTerminal("comma_expr_opt");
            var comma_string_lit_opt = new NonTerminal("comma_string_lit_opt");


            var member_file  = new NonTerminal("member_file");
            var file_hearder = new NonTerminal("file_header");

            var map           = new NonTerminal("map");
            var map_opt       = new NonTerminal("map_opt");
            var map_elem      = new NonTerminal("map_elem");
            var map_elem_list = new NonTerminal("map_elem_list");

            var proc_decl_list     = new NonTerminal("proc_decl_list");
            var proc_decl          = new NonTerminal("proc_decl");
            var proc_impl_list     = new NonTerminal("proc_impl_list");
            var proc_impl          = new NonTerminal("proc_impl");
            var data_decl_list     = new NonTerminal("data_decl_list");
            var data_decl_list_opt = new NonTerminal("data_decl_list_opt");
            var data_decl          = new NonTerminal("data_decl");
            var stmt_list          = new NonTerminal("stmt_list");
            var stmt_line          = new NonTerminal("stmt_line");
            var stmt        = new NonTerminal("stmt");
            var assign_stmt = new NonTerminal("assign_stmt");
            var expr        = new NonTerminal("expr");
            var expr_opt    = new NonTerminal("expr_opt");
            var bin_expr    = new NonTerminal("bin_expr");
            var bin_op      = new NonTerminal("bin_op");
            var fun_call    = new NonTerminal("fun_call");
            var par_expr    = new NonTerminal("par_expr");
            var expr_list   = new NonTerminal("expr_list");
            var term        = new NonTerminal("term");

            var module_decl    = new NonTerminal("module_decl");
            var module_header  = new NonTerminal("module_ref");
            var data_type      = new NonTerminal("data_type");
            var param_init_opt = new NonTerminal("param_init_opt");
            var type_args_opt  = new NonTerminal("type_args_opt");
            var type_arg_list  = new NonTerminal("type_arg_list");
            var type_arg       = new NonTerminal("type_arg");

            var label_opt          = new NonTerminal("label_opt");
            var param_list         = new NonTerminal("param_list");
            var param              = new NonTerminal("param");
            var param_list_par_opt = new NonTerminal("param_list_par_opt");
            var attr_list_tail_opt = new NonTerminal("attr_list_tail_opt");
            var attr_list          = new NonTerminal("attr_list");
            var attr_def           = new NonTerminal("attr_def");
            var return_line_opt    = new NonTerminal("return_line_opt");
            var end_line           = new NonTerminal("end_line");

            //Rules
            base.Root        = source_file;
            source_file.Rule = main_file | member_file;

            //whitespace - includes compiler directives
            wspace.Rule   = NewLine + ws_lines;// +ReduceHere();
            ws_lines.Rule = MakeStarRule(ws_lines, ws_line);
            ws_line.Rule  = compiler_dir | PreferShiftHere() + NewLine + ReduceHere();
            //compiler_dir_line.Rule = compiler_dir;
            compiler_dir.Rule         = cdir_include | cdir_equate | cdir_compile_omit | cdir_section | cdir_compile_end_tag;
            cdir_include.Rule         = ToTerm("INCLUDE") + "(" + string_lit + comma_string_lit_opt + ")" + once_opt;
            comma_string_lit_opt.Rule = Empty | comma + string_lit;
            once_opt.Rule             = comma + "ONCE" | Empty;
            cdir_equate.Rule          = //ShiftIf("EQUATE").ComesBefore(NewLine)  +
                                        label + "EQUATE" + "(" + const_value + ")";
            const_value.Rule       = number | string_lit;
            cdir_compile_omit.Rule = compile_or_omit + "(" + string_lit + comma_expr_opt + ")";
            comma_expr_opt.Rule    = Empty | comma + expr;
            cdir_section.Rule      = ToTerm("SECTION") + "(" + string_lit + ")";
            compile_or_omit.Rule   = ToTerm("COMPILE") | "OMIT";

            //File structure
            main_file.Rule = "PROGRAM" + wspace + map + data_decl_list_opt + wspace +
                             "CODE" + wspace + stmt_list + return_line_opt + proc_impl_list;
            member_file.Rule = "MEMBER" + wspace + map_opt + data_decl_list + proc_impl_list;

            //map
            map_opt.Rule       = map | Empty;
            map.Rule           = "MAP" + wspace + map_elem_list + end_line;
            map_elem_list.Rule = MakeStarRule(map_elem_list, wspace, map_elem);
            map_elem.Rule      = proc_decl | module_header;
            module_decl.Rule   = module_header + proc_decl_list + end_line;
            module_header.Rule = ToTerm("MODULE") + "(" + string_lit + ")" + wspace;

            proc_decl_list.Rule     = MakePlusRule(proc_decl_list, proc_decl);
            proc_decl.Rule          = label + "PROCEDURE" + param_list_par_opt + attr_list_tail_opt + wspace;
            param_list_par_opt.Rule = Empty | "(" + param_list + ")";
            param_list.Rule         = MakePlusRule(param_list, comma, param);
            param.Rule          = data_type + identifier + param_init_opt;
            param_init_opt.Rule = Empty | "=" + number;
            data_type.Rule      = identifier + type_args_opt;
            type_args_opt.Rule  = Empty | "(" + type_arg_list + ")";
            type_arg_list.Rule  = MakePlusRule(type_arg_list, comma, type_arg);
            type_arg.Rule       = number | identifier;

            attr_list_tail_opt.Rule = Empty | comma + attr_list;
            attr_list.Rule          = MakePlusRule(attr_list, comma, attr_def);
            attr_def.Rule           = identifier + param_list_par_opt;

            data_decl_list.Rule     = MakePlusRule(data_decl_list, data_decl);
            data_decl_list_opt.Rule = data_decl_list | Empty;
            data_decl.Rule          = identifier + data_type + wspace;

            proc_impl_list.Rule = MakeStarRule(proc_impl_list, proc_impl);
            proc_impl.Rule      = proc_decl + data_decl_list + "CODE" + wspace + stmt_list + return_line_opt + wspace;
            stmt_list.Rule      = MakeStarRule(stmt_list, stmt_line);
            stmt_line.Rule      = stmt + wspace;
            stmt.Rule           = assign_stmt | fun_call;
            assign_stmt.Rule    = identifier + "=" + expr;
            expr.Rule           = fun_call | par_expr | bin_expr | term;
            par_expr.Rule       = "(" + expr + ")";
            bin_expr.Rule       = expr + bin_op + expr;
            bin_op.Rule         = ToTerm("+") | "-" | "*" | "/" | "^" | "%"
                                  | "&"
                                  | "=" | "<" | ">" | "~=" | "~<" | "~>" | not + "=" | not + "<" | not + ">"
                                  | "<>" | "<=" | "=<" | ">=" | "=>"
                                  | "~" | not | "AND" | "OR" | "XOR";
            fun_call.Rule  = identifier + "(" + expr_list + ")";
            expr_list.Rule = MakeStarRule(expr_list, comma, expr);
            expr_opt.Rule  = expr | Empty;
            term.Rule      = identifier | number | string_lit;

            return_line_opt.Rule = "RETURN" + expr_opt | Empty;
            end_line.Rule        = "END" + NewLine | "." + NewLine;

            //operator precedence
            RegisterOperators(10, "OR", "XOR");
            RegisterOperators(20, "AND");

            RegisterOperators(50, "=", "<", ">", "~=", "~<", "~>", "<>", "<=", "=<", ">=", "=>");
            RegisterOperators(100, "+", "-");
            RegisterOperators(110, "*", "/", "%", "&");
            RegisterOperators(120, Associativity.Right, "^");
            RegisterOperators(130, not);
            RegisterOperators(130, "~");

            //punctuation, brace pairs, transient nodes
            MarkPunctuation("(", ")", ",");
            RegisterBracePair("(", ")");

            //Reserved words and special words
            var resWordList = "ACCEPT AND BEGIN BREAK BY CASE CHOOSE COMPILE CYCLE DO ELSE ELSIF END EXECUTE EXIT FUNCTION GOTO IF INCLUDE LOOP" +
                              " MEMBER NEW NOT NULL OF OMIT OR OROF PARENT PROCEDURE PROGRAM RETURN ROUTINE SECTION SELF THEN TIMES TO UNTIL WHILE XOR";

            this.MarkReservedWords(resWordList.Split(' '));
            var specialWordsList = "APPLICATION CLASS CODE DATA DETAIL FILE FOOTER FORM GROUP HEADER ITEM ITEMIZE JOIN MAP MENU MENUBAR" +
                                   " MODULE OLECONTROL OPTION QUEUE RECORD REPORT ROW SHEET TAB TABLE TOOLBAR VIEW WINDOW";

            //Initialize special words list (words that cannot be used as proc names); we'll later use them for verifying proc names
            SpecialWords = new HashSet <string>(StringComparer.InvariantCultureIgnoreCase);
            SpecialWords.UnionWith(specialWordsList.Split(' '));
        }
Esempio n. 12
0
        public CalcGrammar() : base(true)
        {
            var comment      = new CommentTerminal("comment", "//", "\n", "\r");
            var blockComment = new CommentTerminal("blockComment", "/*", "*/");

            NonGrammarTerminals.Add(comment);
            NonGrammarTerminals.Add(blockComment);

            NonTerminal start                   = new NonTerminal("start", typeof(StatementListNode));
            NonTerminal block                   = new NonTerminal("block");
            NonTerminal instructions            = new NonTerminal("instructions", typeof(BlockNode));
            NonTerminal instruction             = new NonTerminal("instruction");
            NonTerminal embeddedInstruction     = new NonTerminal("embeddedInstruction");
            NonTerminal ifClause                = new NonTerminal("ifClause", typeof(IfNode));
            NonTerminal ifElseClause            = new NonTerminal("ifElseClause", typeof(IfNode));
            NonTerminal forClause               = new NonTerminal("forClause", typeof(ForNode));
            NonTerminal forInitClause           = new NonTerminal("forInit", typeof(StatementListNode));
            NonTerminal forConditionClause      = new NonTerminal("forCondition");
            NonTerminal forIterClause           = new NonTerminal("forIter", typeof(StatementListNode));
            NonTerminal foreachClause           = new NonTerminal("foreachClause", typeof(ForeachNode));
            NonTerminal foreachVarDecl          = new NonTerminal("foreachVarDecl");
            NonTerminal whileClause             = new NonTerminal("whileClause", typeof(WhileNode));
            NonTerminal doWhileClause           = new NonTerminal("doWhileClause", typeof(DoWhileNode));
            NonTerminal returnClause            = new NonTerminal("returnClause", typeof(ReturnNode));
            NonTerminal emptyReturnClause       = new NonTerminal("emptyReturnClause", typeof(ReturnNode));
            NonTerminal breakClause             = new NonTerminal("breakClause", typeof(BreakNode));
            NonTerminal continueClause          = new NonTerminal("continueClause", typeof(ContinueNode));
            NonTerminal usingClause             = new NonTerminal("usingClause", typeof(UsingNode));
            NonTerminal usingNamespace          = new NonTerminal("namespace", typeof(UsingNamespaceNode));
            NonTerminal tryClause               = new NonTerminal("tryClause", typeof(TryNode));
            NonTerminal catchClause             = new NonTerminal("catchClause", typeof(CatchNode));
            NonTerminal finallyClause           = new NonTerminal("finallyClause");
            NonTerminal throwClause             = new NonTerminal("throwClause", typeof(ThrowNode));
            NonTerminal assignment              = new NonTerminal("assignment", typeof(AssignmentNode));
            NonTerminal assignmentOp            = new NonTerminal("assignmentOp", "assignment operator");
            NonTerminal varDeclaration          = new NonTerminal("varDeclaration", typeof(VarDeclarationNode));
            NonTerminal varDeclarationAndAssign = new NonTerminal("varDeclaration", typeof(VarDeclarationNode));
            NonTerminal functionDef             = new NonTerminal("functionDef", typeof(FunctionDefNode));
            NonTerminal functionBody            = new NonTerminal("functionBody", typeof(StatementListNode));
            NonTerminal lambdaBody              = new NonTerminal("lambdaBody", typeof(StatementListNode));
            NonTerminal inlineFunctionDef       = new NonTerminal("inlineFunctionDef", typeof(LambdaNode));
            NonTerminal externFunctionDef       = new NonTerminal("externFunctionDef", typeof(ExternFunctionNode));
            NonTerminal paramList               = new NonTerminal("paramList", typeof(ParamListNode));
            NonTerminal param                   = new NonTerminal("param", typeof(ParamNode));
            NonTerminal lambdaParamList         = new NonTerminal("lambdaParamList", typeof(ParamListNode));
            NonTerminal singleLambdaParamList   = new NonTerminal("lambdaParamList", typeof(ParamListNode));
            NonTerminal lambdaParam             = new NonTerminal("lambdaParam", typeof(ParamNode));
            NonTerminal paramsOrEmpty           = new NonTerminal("paramsOrEmpty");
            NonTerminal arrayDef                = new NonTerminal("arrayDef");
            NonTerminal arrayDefList            = new NonTerminal("arrayDefList", typeof(ArrayDefNode));
            NonTerminal arrayDefListItem        = new NonTerminal("arrayDefListItem");
            NonTerminal namedArrayItem          = new NonTerminal("namedArrayItem", typeof(NamedArrayItemNode));
            NonTerminal expr                   = new NonTerminal("expr");
            NonTerminal prefixExpr             = new NonTerminal("prefixExpr", typeof(IncDecNode));
            NonTerminal postfixExpr            = new NonTerminal("postfixExpr", typeof(IncDecNode));
            NonTerminal binExpr                = new NonTerminal("binExpr", typeof(BinaryOperationNode));
            NonTerminal unExpr                 = new NonTerminal("unExpr", typeof(UnaryExpressionNode));
            NonTerminal var                    = new NonTerminal("var");
            NonTerminal objRef                 = new NonTerminal("objRef");
            NonTerminal memberAccess           = new NonTerminal("memberAccess", typeof(MemberAccessNode));
            NonTerminal ternaryIf              = new NonTerminal("ternaryIf", typeof(IfNode));
            NonTerminal coalescence            = new NonTerminal("coalescence", typeof(CoalescenceNode));
            NonTerminal functionCall           = new NonTerminal("functionCall", typeof(FunctionCallNode));
            NonTerminal varList                = new NonTerminal("varList", typeof(ExpressionListNode));
            NonTerminal array                  = new NonTerminal("array");
            NonTerminal singleDimArray         = new NonTerminal("singleDimArray", typeof(IndexedAccessNode));
            NonTerminal rangeArrayDef          = new NonTerminal("rangeArrayDef", typeof(RangeArrayDefNode));
            NonTerminal rangeInclusiveArrayDef = new NonTerminal("rangeInclusiveArrayDef", typeof(RangeArrayDefNode));
            NonTerminal typeInfo               = new NonTerminal("typeInfo");
            NonTerminal typeInfoOrEmpty        = new NonTerminal("typeInfoOrEmpty");

            IdentifierTerminal name    = new IdentifierTerminal("name", IdOptions.IsNotKeyword);
            IdentifierTerminal newName = new IdentifierTerminal("newName", IdOptions.IsNotKeyword);
            NumberLiteral      number  = new NumberLiteral("number", NumberOptions.AllowUnderscore);

            StringLiteral _string = new StringLiteral("string", "\"", StringOptions.AllowsAllEscapes);

            StringLiteral _char = new StringLiteral("Char", "'", StringOptions.IsChar | StringOptions.AllowsAllEscapes);

            NonTerminal boolVal  = new NonTerminal("boolVal", typeof(BoolValNode));
            NonTerminal nullVal  = new NonTerminal("nullVal", typeof(NullValueNode));
            NonTerminal thisVal  = new NonTerminal("thisVal", typeof(ThisNode));
            NonTerminal binOp    = new NonTerminal("binOp", "operator");
            NonTerminal unaryOp  = new NonTerminal("unaryOp", "operator");
            NonTerminal incDecOp = new NonTerminal("incDecOp", "operator");

            NonTerminal emptyInstruction = new NonTerminal("emptyInstruction", typeof(EmptyNode));

            start.Rule        = MakeStarRule(start, instruction);
            block.Rule        = "{" + instructions + "}";
            instructions.Rule = MakeStarRule(instructions, instruction);
            instruction.Rule  = block
                                | embeddedInstruction + ";"
                                | ifClause
                                | ifElseClause
                                | functionDef
                                | externFunctionDef + ";"
                                | returnClause + ";"
                                | emptyReturnClause
                                | breakClause
                                | continueClause
                                | forClause
                                | foreachClause
                                | whileClause
                                | doWhileClause
                                | usingClause
                                | varDeclaration + ";"
                                | emptyInstruction
                                | tryClause
                                | throwClause + ";";
            emptyInstruction.Rule    = ToTerm(";");
            instruction.ErrorRule    = SyntaxError + ";";
            embeddedInstruction.Rule = functionCall | postfixExpr | prefixExpr | assignment | varDeclarationAndAssign;
            ifElseClause.Rule        = ToTerm("if") + "(" + expr + ")" + instruction
                                       + PreferShiftHere() + "else" + instruction;
            ifClause.Rule           = ToTerm("if") + "(" + expr + ")" + instruction;
            forClause.Rule          = ToTerm("for") + "(" + forInitClause + ";" + forConditionClause + ";" + forIterClause + ")" + instruction;
            forInitClause.Rule      = MakeStarRule(forInitClause, ToTerm(","), embeddedInstruction);
            forConditionClause.Rule = Empty | expr;
            forIterClause.Rule      = MakeStarRule(forIterClause, ToTerm(","), embeddedInstruction);
            foreachClause.Rule      = ToTerm("foreach") + "(" + foreachVarDecl + "in" + expr + ")" + instruction;
            foreachVarDecl.Rule     = varDeclaration | name;
            whileClause.Rule        = ToTerm("while") + "(" + expr + ")" + instruction;
            doWhileClause.Rule      = ToTerm("do") + instruction + ToTerm("while") + "(" + expr + ")" + ToTerm(";");
            returnClause.Rule       = "return" + expr;
            emptyReturnClause.Rule  = ToTerm("return") + ";";
            breakClause.Rule        = ToTerm("break") + ";";
            continueClause.Rule     = ToTerm("continue") + ";";

            tryClause.Rule = "try" + block + (catchClause + finallyClause | finallyClause | catchClause);

            catchClause.Rule   = "catch" + ("(" + name + ")").Q() + block;
            finallyClause.Rule = "finally" + block;

            throwClause.Rule = "throw" + expr;

            usingClause.Rule = ToTerm("using") + usingNamespace + ";";

            usingNamespace.Rule = (name + "." + usingNamespace) | name;

            varDeclaration.Rule          = "var" + name + typeInfoOrEmpty;
            varDeclarationAndAssign.Rule = "var" + name + typeInfoOrEmpty + "=" + expr;

            assignment.Rule   = objRef + assignmentOp + expr;
            assignmentOp.Rule = ToTerm("=") | "+=" | "-=" | "*=" | "/=" | "%=" | "|=" | "^=" | "&=" | "<<=" | ">>=" | "**=";
            objRef.Rule       = name | array | memberAccess;
            memberAccess.Rule = var + PreferShiftHere() + "." + name;

            functionDef.Rule = "function" + name + "(" + paramList + ")" + ToTerm("extension").Q() + functionBody;
            functionDef.NodeCaptionTemplate = "function #{0}(...)";
            inlineFunctionDef.Rule          = (ToTerm("function") + "(" + paramList + ")" + functionBody)
                                              | ("(" + lambdaParamList + ")" + ToTerm("=>") + expr)
                                              | (singleLambdaParamList + "=>" + expr);
            externFunctionDef.Rule = ToTerm("extern") + "function" + name + "(" + paramList + ")" + ToTerm("extension").Q();
            inlineFunctionDef.NodeCaptionTemplate = "function(...)";
            functionBody.Rule = block | returnClause;

            paramList.Rule             = MakeStarRule(paramList, ToTerm(","), param);
            lambdaParamList.Rule       = MakeStarRule(lambdaParamList, ToTerm(","), lambdaParam);
            singleLambdaParamList.Rule = lambdaParam;

            lambdaParam.Rule   = name + ReduceIf("=>", "+", "-", "*", "/", "%", "**", "&", "&&", "|", "||", "^", "==", "<=", ">=", "<", ">", "!=", "<<", ">>", ";", "(", "??");
            param.Rule         = paramsOrEmpty + name + typeInfoOrEmpty;
            paramsOrEmpty.Rule = ToTerm("params") | Empty;

            arrayDef.Rule         = "{" + arrayDefList + "}";
            arrayDefList.Rule     = MakeStarRule(arrayDefList, ToTerm(","), arrayDefListItem);
            arrayDefListItem.Rule = namedArrayItem | expr;
            namedArrayItem.Rule   = (name + ReduceHere() | _string) + "=" + expr;

            rangeArrayDef.Rule = expr + PreferShiftHere() + ".." + expr + ((PreferShiftHere() + ":" + expr) | Empty);

            rangeInclusiveArrayDef.Rule = expr + PreferShiftHere() + "..." + expr + ((PreferShiftHere() + ":" + expr) | Empty);

            expr.Rule = prefixExpr | postfixExpr | ternaryIf
                        | inlineFunctionDef
                        | var | unExpr | binExpr
                        | arrayDef
                        | rangeArrayDef
                        | rangeInclusiveArrayDef
                        | assignment
                        | coalescence;

            coalescence.Rule = expr + "??" + expr;

            binExpr.Rule = expr + binOp + expr;
            binOp.Rule   = ToTerm("&&") | "||" | "&" | "|" | "^"
                           | ToTerm("==") | "<=" | ">=" | "<" | ">" | "!="
                           | ToTerm("+") | "-"
                           | ToTerm("*") | "/" | "%" | "**"
                           | ToTerm("<<") | ">>";
            prefixExpr.Rule  = incDecOp + objRef + ReduceHere();
            postfixExpr.Rule = objRef + PreferShiftHere() + incDecOp;
            unExpr.Rule      = unaryOp + expr + ReduceHere();

            var.Rule = objRef
                       | number
                       | boolVal
                       | nullVal
                       | thisVal
                       | _string
                       | _char
                       | functionCall + ReduceHere()
                       | ("(" + expr + ")");

            ternaryIf.Rule    = expr + "?" + expr + ":" + expr;
            functionCall.Rule = var + PreferShiftHere() + "(" + varList + ")";
            functionCall.NodeCaptionTemplate = "call #{0}(...)";
            varList.Rule = MakeStarRule(varList, ToTerm(","), expr);

            array.Rule          = singleDimArray;
            singleDimArray.Rule = var + PreferShiftHere() + "[" + expr + "]";

            boolVal.Rule = ToTerm("true") | "false";
            nullVal.Rule = ToTerm("null");
            thisVal.Rule = ToTerm("this");

            typeInfoOrEmpty.Rule = ":" + typeInfo | Empty;
            typeInfo.Rule        = ToTerm("string")
                                   | "function"
                                   | "number"
                                   | "bool"
                                   | "table"
                                   | "char";


            unaryOp.Rule  = ToTerm("-") | "!" | "~";
            incDecOp.Rule = ToTerm("++") | "--";

            MarkPunctuation("(", ")", "?", ":", "[", "]", ";", "{", "}", ".", ",", "@", "=>", "??",
                            "return", "if", "else", "for", "while", "break", "continue",
                            "using", "do", "var", "foreach", "in",
                            "try", "catch", "finally", "throw", "extern");
            RegisterBracePair("(", ")");
            RegisterBracePair("[", "]");
            RegisterBracePair("{", "}");

            RegisterOperators(10, "?");
            RegisterOperators(15, "&&", "||", "&", "|", "^");
            RegisterOperators(20, "==", "<", "<=", ">", ">=", "!=");
            RegisterOperators(25, "<<", ">>");
            RegisterOperators(30, "+", "-");
            RegisterOperators(40, "*", "/", "%", "**");
            RegisterOperators(60, "!", "~");
            RegisterOperators(70, "++", "--", "??");
            MarkTransient(var, expr, binOp, unaryOp, block, instruction, embeddedInstruction, objRef, array, arrayDef, assignmentOp, arrayDefListItem, incDecOp, functionBody, lambdaBody, foreachVarDecl, paramsOrEmpty, typeInfoOrEmpty);

            AddTermsReportGroup("assignment", "=", "+=", "-=", "*=", "/=", "%=", "&=", "|=", "^=", "<<=", ">>=");
            AddTermsReportGroup("statement", "if", "while", "for", "return", "break", "continue", "using", "do", "try", "throw", "foreach");
            AddTermsReportGroup("variable declaration", "var");
            AddTermsReportGroup("function declaration", "function", "extern");
            AddTermsReportGroup("constant", number, _string, _char);
            AddTermsReportGroup("constant", "null", "false", "true", "this", "@");
            AddTermsReportGroup("unary operator", "+", "-", "!");
            AddTermsReportGroup("operator", "+", "-", "*", "/", "%", "**", "&", "&&", "|", "||", "^", "?", "==", "<=", "<", ">=", ">", "!=", "<<", ">>", "??", "..");
            AddToNoReportGroup("(", "[", "{", ".", ",", "++", "--");

            MarkReservedWords("if", "else", "return", "function", "while",
                              "for", "null", "false", "true", "this", "break", "continue",
                              "using", "do", "var", "foreach", "in", "params",
                              "try", "catch", "finally", "throw", "extern");

            number.DefaultFloatType = TypeCode.Double;
            number.DefaultIntTypes  = new TypeCode[] { TypeCode.Int64 };
            number.AddPrefix("0x", NumberOptions.Hex);
            number.AddPrefix("0b", NumberOptions.Binary);
            number.AddSuffix("d", TypeCode.Double);
            number.AddSuffix("l", TypeCode.Int64);
            number.AddSuffix("m", TypeCode.Decimal);

            _string.AddPrefix("@", StringOptions.NoEscapes);
            _string.AddPrefix("$", StringOptions.IsTemplate | StringOptions.AllowsAllEscapes);

            var stringTemplateSettings = new StringTemplateSettings();

            stringTemplateSettings.StartTag       = "{";
            stringTemplateSettings.EndTag         = "}";
            stringTemplateSettings.ExpressionRoot = expr;
            this.SnippetRoots.Add(expr);
            _string.AstConfig.NodeType = typeof(StringTemplateNode);
            _string.AstConfig.Data     = stringTemplateSettings;

            this.Root = start;

            this.LanguageFlags = LanguageFlags.CreateAst;
        }
Esempio n. 13
0
        public IronyArchitectureGrammar()
            : base(false)
        {
            var dot   = ToTerm(".");
            var colon = ToTerm(":");
            var equal = ToTerm("=");
            var push  = ToTerm("[");
            var pop   = ToTerm("]");

            var ID       = TerminalFactory.CreateCSharpIdentifier(IDName); // IdentifierTerminal?
            var STRING   = new StringLiteral(StringName, "\"", StringOptions.AllowsAllEscapes);
            var NUMBER   = new NumberLiteral(NumberName, NumberOptions.AllowSign);
            var VARIABLE = TerminalFactory.CreateCSharpIdentifier(VariableName);

            NUMBER.AddSuffix("r", System.TypeCode.Single);

            var comment = new CommentTerminal(CommentName, "#", "\r", "\n", "\u2085", "\u2028", "\u2029");

            NonGrammarTerminals.Add(comment);

            NonTerminal program              = new NonTerminal(ProgramName),
                        line                 = new NonTerminal(LineName),
                        assignmentStatement  = new NonTerminal(AssignmentStatementName),
                        ruleStatement        = new NonTerminal(RuleStatementName),
                        predecessorStatement = new NonTerminal(PredecessorStatementName),
                        predecessor          = new NonTerminal(PredecessorName),
                        successorList        = new NonTerminal(SuccessorListName),
                        successor            = new NonTerminal(SuccessorName),
                        command              = new NonTerminal(CommandName),
                        argumentList         = new NonTerminal(ArgumentListName),
                        arg          = new NonTerminal(ArgName),
                        namedArg     = new NonTerminal(NamedArgName),
                        atom         = new NonTerminal(AtomName),
                        commandBlock = new NonTerminal(CommandBlockName),
                        ruleList     = new NonTerminal(RuleListName),
                        ruleSymbol   = new NonTerminal(RuleSymbolName),
                        scopeCmd     = new NonTerminal(ScopeCommandName),
                        simpleCmd    = new NonTerminal(SimpleCommandName),
                        probability  = new NonTerminal(ProbabilityName);

            program.Rule              = MakePlusRule(program, line);
            line.Rule                 = assignmentStatement | ruleStatement;
            assignmentStatement.Rule  = "let" + VARIABLE + equal + atom + ";";
            predecessorStatement.Rule = predecessor | Empty;
            predecessor.Rule          = ID + "(" + argumentList + ")" | ID;
            ruleStatement.Rule        = predecessorStatement + ToTerm("::-") + successorList + probability + ";";
            successorList.Rule        = MakePlusRule(successorList, successor);
            successor.Rule            = command | ruleSymbol;
            scopeCmd.Rule             = "Scope" + dot + ID;
            simpleCmd.Rule            = push | pop;
            command.Rule              = simpleCmd | scopeCmd + "(" + argumentList + ")" + commandBlock;
            argumentList.Rule         = MakeStarRule(argumentList, ToTerm(","), arg);
            arg.Rule          = atom | namedArg;
            namedArg.Rule     = VARIABLE + colon + atom;
            atom.Rule         = NUMBER | STRING | VARIABLE;
            commandBlock.Rule = ToTerm("{") + ruleList + ToTerm("}") | Empty;
            ruleSymbol.Rule   = ID + "(" + argumentList + ")" | ID;
            ruleList.Rule     = MakeStarRule(ruleList, ToTerm("|"), ruleSymbol);
            probability.Rule  = Empty | (colon + NUMBER);

            this.Root = program;

            MarkTransient(ruleList, commandBlock);

            MarkPunctuation("::-", ",", "(", ")", "{", "}", ";", ":", "=", "let", ".");
        }
Esempio n. 14
0
		public RefalGrammar() : base(true) // case sensitive
		{
			GrammarComments = "Refal-5 language interpreter based on Irony toolkit.";

			// Terminals
			var Number = new NumberLiteral("Number");
			Number.DefaultIntTypes = new TypeCode[] { TypeCode.Int32, TypeCode.Int64, NumberLiteral.TypeCodeBigInt };
			Number.AddPrefix("0x", NumberOptions.Hex);
			Number.AddSuffix("u", TypeCode.UInt32);
			Number.AddSuffix("l", TypeCode.Int64);
			Number.AddSuffix("ul", TypeCode.UInt64);
			Number.Options |= NumberOptions.IntOnly;

			var CharLiteral = new StringLiteral("Char", "'", StringOptions.AllowsAllEscapes);
			var StringLiteral = new StringLiteral("String", "\"", StringOptions.AllowsAllEscapes);
			var Identifier = new IdentifierTerminal("Identifier", "_-", "");

			var LineComment = new CommentTerminal("LineComment", "*", "\n", "\r");
			LineComment.ValidateToken += LineComment_ValidateToken;

			var BlockComment = new CommentTerminal("BlockComment", "/*", "*/");
			NonGrammarTerminals.Add(LineComment);
			NonGrammarTerminals.Add(BlockComment);

			// Non-terminals
			var Program = new NonTerminal("Program", typeof(Program));
			var Definition = new NonTerminal("Definition");
			var Function = new NonTerminal("Function", typeof(DefinedFunction));
			var External = new NonTerminal("External", typeof(AuxiliaryNode));
			var IdentifierList = new NonTerminal("Identifier+", typeof(AuxiliaryNode));
			var Block = new NonTerminal("Block", typeof(Block));
			var Sentence = new NonTerminal("Sentence", typeof(Sentence));
			var RSentence = new NonTerminal("RSentence", typeof(AuxiliaryNode));
			var SentenceList = new NonTerminal("Sentence+", typeof(AuxiliaryNode));
			var Pattern = new NonTerminal("Pattern", typeof(Pattern));
			var PatternItem = new NonTerminal("PatternItem");
			var PatternInParentheses = new NonTerminal("(Pattern)", typeof(ExpressionInBraces));
			var Expression = new NonTerminal("Expression", typeof(Expression));
			var ExpressionItem = new NonTerminal("ExpressionItem");
			var ExpressionInParentheses = new NonTerminal("(Expression)", typeof(ExpressionInBraces));
			var Var = new NonTerminal("Variable", Variable.CreateVariableNode);
			var VarPrefix = new NonTerminal("VariablePrefix");
			var VarIndex = new NonTerminal("VariableIndex");
			var Symbol = new NonTerminal("Symbol", LiteralValueNodeHelper.InitNode);
			var Call = new NonTerminal("Call", typeof(FunctionCall));
			var FunctionName = new NonTerminal("FunctionName", typeof(AuxiliaryNode));
			var WhereOrWithClause = new NonTerminal("WhereOrWithClause", typeof(Conditions));
			var CommaOrAmpersand = new NonTerminal("CommaOrAmpersand");
			var RWhereOrWithClause = new NonTerminal("RWhereOrWithClause", typeof(AuxiliaryNode));
			var RExpressionOrWhereOrWithClause = new NonTerminal("RExpressionOrWhereOrWithClause");

			var SemicolonOpt = new NonTerminal("[;]", Empty | ";");
			var EntryOpt = new NonTerminal("[ENTRY]", Empty | "$ENTRY");
			var Extern = new NonTerminal("Extern", ToTerm("$EXTRN") | "$EXTERN" | "$EXTERNAL");

			// Rules
			Root = Program;

			Program.Rule = MakePlusRule(Program, Definition);
			Definition.Rule = Function | External;
			External.Rule = Extern + IdentifierList + ";";
			IdentifierList.Rule = MakePlusRule(IdentifierList, ToTerm(","), Identifier);

			Function.Rule = EntryOpt + Identifier + Block + SemicolonOpt;
			Block.Rule = "{" + SentenceList + SemicolonOpt + "}";
			SentenceList.Rule = MakePlusRule(SentenceList, ToTerm(";"), Sentence);

			Sentence.Rule = Pattern + RSentence;
			RSentence.Rule = "=" + Expression | WhereOrWithClause;
			Pattern.Rule = MakeStarRule(Pattern, PatternItem);
			PatternItem.Rule = Var | Symbol | PatternInParentheses;
			PatternInParentheses.Rule = "(" + Pattern + ")";
			Expression.Rule = MakeStarRule(Expression, ExpressionItem);
			ExpressionItem.Rule = Call | Var | Symbol | ExpressionInParentheses;
			ExpressionInParentheses.Rule = "(" + Expression + ")";

			Var.Rule = VarPrefix + "." + VarIndex;
			VarPrefix.Rule = ToTerm("e") | "s" | "t";
			VarIndex.Rule = Number | Identifier;
			Symbol.Rule = StringLiteral | CharLiteral | Number | "True" | "False" | Identifier;
			Call.Rule = "<" + FunctionName + Expression + ">";
			FunctionName.Rule = Identifier | "+" | "-" | "*" | "/";

			WhereOrWithClause.Rule = CommaOrAmpersand + Expression + ":" + RWhereOrWithClause;
			CommaOrAmpersand.Rule = ToTerm(",") | "&";
			RWhereOrWithClause.Rule = Block // with-clause
				| Pattern + RExpressionOrWhereOrWithClause; // where-clause
			RExpressionOrWhereOrWithClause.Rule = ToTerm("=") + Expression | WhereOrWithClause;

			// Punctuation, braces, transient terms, options
			MarkPunctuation("(", ")");
			MarkPunctuation("{", "}");
			MarkPunctuation("<", ">");
			MarkPunctuation("=", ",", "&", ";");

			RegisterBracePair("(", ")");
			RegisterBracePair("<", ">");
			RegisterBracePair("{", "}");

			MarkTransient(Definition, PatternItem, ExpressionItem, SemicolonOpt, EntryOpt, Extern, CommaOrAmpersand, VarPrefix, VarIndex, RExpressionOrWhereOrWithClause);
			LanguageFlags = LanguageFlags.CreateAst | LanguageFlags.SupportsBigInt | LanguageFlags.TailRecursive;
		}
Esempio n. 15
0
 //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);
   term.DefaultIntTypes = new TypeCode[] { TypeCode.Int32, TypeCode.Int64 };
   //term.DefaultFloatType = TypeCode.Double; it is default
   term.AddPrefix("&H", NumberOptions.Hex);
   term.AddPrefix("&O", NumberOptions.Octal);
   term.AddSuffix("S", TypeCode.Int16);
   term.AddSuffix("I", TypeCode.Int32);
   term.AddSuffix("%", TypeCode.Int32);
   term.AddSuffix("L", TypeCode.Int64);
   term.AddSuffix("&", TypeCode.Int64);
   term.AddSuffix("D", TypeCode.Decimal);
   term.AddSuffix("@", TypeCode.Decimal);
   term.AddSuffix("F", TypeCode.Single);
   term.AddSuffix("!", TypeCode.Single);
   term.AddSuffix("R", TypeCode.Double);
   term.AddSuffix("#", TypeCode.Double);
   term.AddSuffix("US", TypeCode.UInt16);
   term.AddSuffix("UI", TypeCode.UInt32);
   term.AddSuffix("UL", TypeCode.UInt64);
   return term;
 }
Esempio n. 16
0
    public ClarionGrammar() : base(false) { //case insensitive
      base.GrammarComments = "Clarion grammar for parsing Clarion sources.";

      //Terminals
      var comment = new CommentTerminal("comment", "!", "\n");
      this.NonGrammarTerminals.Add(comment);
      _comment = comment; 
      var identifier = new IdentifierTerminal("identifier", ":", string.Empty);
      var label = new IdentifierTerminal("label", ":", string.Empty);
      label.ValidateToken += label_ValidateToken;
      label.Priority = TerminalPriority.High;
      //Clarion uses suffixes for identifying number base (bin, octal, hex) - unlike Irony's NumberLiteral which supports suffixes.
      // We treat all numbers as hexadecimal with optional prefixes that don't mean anything. We "fix" the value in ValidateToken handler
      var number = new NumberLiteral("number");
      number.Options = NumberOptions.Hex | NumberOptions.DisableQuickParse;
      number.AddSuffix("b", TypeCode.Int64);
      number.AddSuffix("o", TypeCode.Int64);
      number.AddSuffix("h", TypeCode.Int64);
      number.ValidateToken += number_ValidateToken;
     
      var string_lit = new StringLiteral("string_lit", "'", StringOptions.AllowsDoubledQuote | StringOptions.NoEscapes);

      var comma = ToTerm(",");
      var not = ToTerm("NOT");
      
      //Non-terminals
      var source_file = new NonTerminal("source_file");
      var main_file = new NonTerminal("main_file");
      //whitespace - NewLine, Empty line, line with compiler directive, or line with end-tag of Compile(..) directive
      var wspace = new NonTerminal("wspace");
      var ws_lines = new NonTerminal("ws_lines");
      var ws_line = new NonTerminal("ws_line");
      var compiler_dir = new NonTerminal("compiler_dir");
      var compiler_dir_line = new NonTerminal("compiler_dir_line");
      var cdir_compile_end_tag = new CustomTerminal("compile_end_tag", Match_compile_end_tag);

      var cdir_include = new NonTerminal("cdir_include");
      var cdir_equate = new NonTerminal("cdir_equate");
      var cdir_compile_omit = new NonTerminal("cdir_compile_omit");
      var compile_or_omit = new NonTerminal("compile_or_omit");
      var cdir_section = new NonTerminal("cdir_section");
      var once_opt = new NonTerminal("once_opt");
      var const_value = new NonTerminal("const_value");
      var comma_expr_opt = new NonTerminal("comma_expr_opt");
      var comma_string_lit_opt = new NonTerminal("comma_string_lit_opt");


      var member_file = new NonTerminal("member_file");
      var file_hearder = new NonTerminal("file_header");

      var map = new NonTerminal("map");
      var map_opt = new NonTerminal("map_opt");
      var map_elem = new NonTerminal("map_elem");
      var map_elem_list = new NonTerminal("map_elem_list");
      
      var proc_decl_list = new NonTerminal("proc_decl_list");
      var proc_decl = new NonTerminal("proc_decl");
      var proc_impl_list = new NonTerminal("proc_impl_list");
      var proc_impl = new NonTerminal("proc_impl");
      var data_decl_list = new NonTerminal("data_decl_list");
      var data_decl_list_opt = new NonTerminal("data_decl_list_opt");
      var data_decl = new NonTerminal("data_decl");
      var stmt_list = new NonTerminal("stmt_list");
      var stmt_line = new NonTerminal("stmt_line");
      var stmt = new NonTerminal("stmt");
      var assign_stmt = new NonTerminal("assign_stmt");
      var expr = new NonTerminal("expr");
      var expr_opt = new NonTerminal("expr_opt");
      var bin_expr = new NonTerminal("bin_expr");
      var bin_op = new NonTerminal("bin_op");
      var fun_call = new NonTerminal("fun_call");
      var par_expr = new NonTerminal("par_expr");
      var expr_list = new NonTerminal("expr_list");
      var term = new NonTerminal("term");

      var module_decl = new NonTerminal("module_decl");
      var module_header = new NonTerminal("module_ref");
      var data_type = new NonTerminal("data_type");
      var param_init_opt = new NonTerminal("param_init_opt");
      var type_args_opt = new NonTerminal("type_args_opt");
      var type_arg_list = new NonTerminal("type_arg_list");
      var type_arg = new NonTerminal("type_arg"); 

      var label_opt = new NonTerminal("label_opt");
      var param_list = new NonTerminal("param_list"); 
      var param = new NonTerminal("param");
      var param_list_par_opt = new NonTerminal("param_list_par_opt");
      var attr_list_tail_opt = new NonTerminal("attr_list_tail_opt");
      var attr_list = new NonTerminal("attr_list");
      var attr_def = new NonTerminal("attr_def");
      var return_line_opt = new NonTerminal("return_line_opt");
      var end_line = new NonTerminal("end_line");
      
      //Rules 
      base.Root = source_file;
      source_file.Rule = main_file | member_file;

      //whitespace - includes compiler directives
      wspace.Rule = NewLine + ws_lines;// +ReduceHere();
      ws_lines.Rule =  MakeStarRule(ws_lines, ws_line);
      ws_line.Rule = compiler_dir | PreferShiftHere() + NewLine + ReduceHere();
      //compiler_dir_line.Rule = compiler_dir;
      compiler_dir.Rule = cdir_include | cdir_equate | cdir_compile_omit | cdir_section | cdir_compile_end_tag;
      cdir_include.Rule = ToTerm("INCLUDE") + "(" + string_lit + comma_string_lit_opt + ")" + once_opt;
      comma_string_lit_opt.Rule = Empty | comma + string_lit;
      once_opt.Rule = comma + "ONCE" | Empty;
      cdir_equate.Rule = //ShiftIf("EQUATE").ComesBefore(NewLine)  + 
        label + "EQUATE" + "(" + const_value + ")";
      const_value.Rule = number | string_lit;
      cdir_compile_omit.Rule = compile_or_omit + "(" + string_lit + comma_expr_opt + ")";
      comma_expr_opt.Rule = Empty | comma + expr;
      cdir_section.Rule = ToTerm("SECTION") + "(" + string_lit + ")";
      compile_or_omit.Rule = ToTerm("COMPILE") | "OMIT";
      
      //File structure
      main_file.Rule = "PROGRAM" + wspace + map + data_decl_list_opt + wspace +
                     "CODE" + wspace + stmt_list + return_line_opt + proc_impl_list;
      member_file.Rule = "MEMBER" + wspace + map_opt + data_decl_list + proc_impl_list;

      //map
      map_opt.Rule = map | Empty;
      map.Rule = "MAP" + wspace + map_elem_list + end_line;
      map_elem_list.Rule = MakeStarRule(map_elem_list, wspace, map_elem);
      map_elem.Rule = proc_decl | module_header;
      module_decl.Rule = module_header + proc_decl_list + end_line;
      module_header.Rule = ToTerm("MODULE") + "(" + string_lit + ")" + wspace; 

      proc_decl_list.Rule = MakePlusRule(proc_decl_list, proc_decl);
      proc_decl.Rule = label + "PROCEDURE" + param_list_par_opt + attr_list_tail_opt + wspace;
      param_list_par_opt.Rule = Empty | "(" + param_list + ")";
      param_list.Rule = MakePlusRule(param_list, comma, param);
      param.Rule = data_type + identifier + param_init_opt;
      param_init_opt.Rule = Empty | "=" + number;
      data_type.Rule = identifier + type_args_opt; 
      type_args_opt.Rule = Empty | "(" + type_arg_list + ")";
      type_arg_list.Rule = MakePlusRule(type_arg_list, comma, type_arg);
      type_arg.Rule = number | identifier; 
      
      attr_list_tail_opt.Rule = Empty | comma + attr_list;
      attr_list.Rule = MakePlusRule(attr_list, comma, attr_def); 
      attr_def.Rule = identifier + param_list_par_opt;

      data_decl_list.Rule = MakePlusRule(data_decl_list, data_decl);
      data_decl_list_opt.Rule = data_decl_list | Empty; 
      data_decl.Rule = identifier + data_type + wspace;

      proc_impl_list.Rule = MakeStarRule(proc_impl_list, proc_impl);
      proc_impl.Rule = proc_decl + data_decl_list + "CODE" + wspace + stmt_list + return_line_opt + wspace;
      stmt_list.Rule = MakeStarRule(stmt_list, stmt_line);
      stmt_line.Rule = stmt + wspace;
      stmt.Rule = assign_stmt | fun_call;
      assign_stmt.Rule = identifier + "=" + expr;
      expr.Rule = fun_call | par_expr | bin_expr | term;
      par_expr.Rule = "(" + expr + ")";
      bin_expr.Rule = expr + bin_op + expr;
      bin_op.Rule = ToTerm("+") | "-" | "*" | "/" | "^" | "%"
                  | "&"
                  | "=" | "<" | ">" | "~=" | "~<" | "~>" | not + "=" | not + "<" | not + ">"
                  | "<>" | "<=" | "=<" | ">=" | "=>"
                  | "~" | not | "AND" | "OR" | "XOR";
      fun_call.Rule = identifier + "(" + expr_list + ")";
      expr_list.Rule = MakeStarRule(expr_list, comma, expr);
      expr_opt.Rule = expr | Empty;
      term.Rule = identifier | number | string_lit;

      return_line_opt.Rule = "RETURN" + expr_opt | Empty;
      end_line.Rule = "END" + NewLine | "." + NewLine;

      //operator precedence 
      RegisterOperators(10,  "OR", "XOR");
      RegisterOperators(20, "AND");

      RegisterOperators(50, "=", "<", ">", "~=", "~<", "~>", "<>", "<=", "=<", ">=", "=>"); 
      RegisterOperators(100, "+", "-");
      RegisterOperators(110, "*", "/", "%", "&");
      RegisterOperators(120, Associativity.Right, "^");
      RegisterOperators(130, not);
      RegisterOperators(130, "~");

      //punctuation, brace pairs, transient nodes
      MarkPunctuation("(", ")", ",");
      RegisterBracePair("(", ")");
      
      //Reserved words and special words
      var resWordList = "ACCEPT AND BEGIN BREAK BY CASE CHOOSE COMPILE CYCLE DO ELSE ELSIF END EXECUTE EXIT FUNCTION GOTO IF INCLUDE LOOP" +
                        " MEMBER NEW NOT NULL OF OMIT OR OROF PARENT PROCEDURE PROGRAM RETURN ROUTINE SECTION SELF THEN TIMES TO UNTIL WHILE XOR";
      this.MarkReservedWords(resWordList.Split(' '));
      var specialWordsList = "APPLICATION CLASS CODE DATA DETAIL FILE FOOTER FORM GROUP HEADER ITEM ITEMIZE JOIN MAP MENU MENUBAR" + 
                         " MODULE OLECONTROL OPTION QUEUE RECORD REPORT ROW SHEET TAB TABLE TOOLBAR VIEW WINDOW";
      //Initialize special words list (words that cannot be used as proc names); we'll later use them for verifying proc names
      SpecialWords = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);
      SpecialWords.UnionWith(specialWordsList.Split(' ')); 
    }
Esempio n. 17
0
        public LiteralRules(PieGrammar grammar)
        {
            this.grammar = grammar;

            NumberLiteral = new NumberLiteral("number_literal", NumberOptions.AllowStartEndDot)
            {
                // Define types to assign if the parser can't determine.
                DefaultIntTypes  = new[] { TypeCode.Int32 },
                DefaultFloatType = TypeCode.Double
            };

            // The pre and post fixes for different number types and formats.
            NumberLiteral.AddPrefix("0x", NumberOptions.Hex);
            NumberLiteral.AddSuffix("b", TypeCode.Byte);
            NumberLiteral.AddSuffix("B", TypeCode.Byte);
            NumberLiteral.AddSuffix("s", TypeCode.Int16);
            NumberLiteral.AddSuffix("S", TypeCode.Int16);
            NumberLiteral.AddSuffix("u", TypeCode.UInt32);
            NumberLiteral.AddSuffix("U", TypeCode.UInt32);
            NumberLiteral.AddSuffix("us", TypeCode.UInt16);
            NumberLiteral.AddSuffix("US", TypeCode.UInt16);
            NumberLiteral.AddSuffix("l", TypeCode.Int64);
            NumberLiteral.AddSuffix("L", TypeCode.Int64);
            NumberLiteral.AddSuffix("ul", TypeCode.UInt64);
            NumberLiteral.AddSuffix("UL", TypeCode.UInt64);
            NumberLiteral.AddSuffix("F", TypeCode.Single);
            NumberLiteral.AddSuffix("f", TypeCode.Single);
            NumberLiteral.AddSuffix("d", TypeCode.Double);
            NumberLiteral.AddSuffix("D", TypeCode.Double);
            NumberLiteral.AddSuffix("m", TypeCode.Decimal);
            NumberLiteral.AddSuffix("M", TypeCode.Decimal);

            StringLiteral = new StringLiteral("string_literal", "\"", StringOptions.AllowsAllEscapes);

            CharLiteral = new StringLiteral("char_literal", "'", StringOptions.IsChar);

            BoolLiteral      = new NonTerminal("bool_literal");
            BoolLiteral.Rule = grammar.Keywords.True | grammar.Keywords.False;

            Null = grammar.ToTerm("null", "null_literal");
        }
Esempio n. 18
0
        public GWBasicGrammar() : base(false)  // BASIC is not case sensitive...
        {
            this.GrammarComments = "This grammar uses one new Irony feature - Scanner-Parser link. Parser helps Scanner to disambiguate " +
                                   "similar but different token types when more than one terminal matches the current input char.\r\n" +
                                   "See comments in GwBasicGrammar.cs file.";

            /*
             * Scanner-Parser link.
             * The grammar defines 3 terminals for numbers: number, lineNumber and fileNumber. All three return decimal
             * digits in GetFirsts() method. So when current input is a digit, the scanner has 3 potential candidate terminals
             * to match the input. However, because each of the 3 terminals can appear in specific "places" in grammar,
             * the parser is able to assist scanner to pick the correct terminal, depending on the current parser state.
             * The disambiguation happens in Scanner.SelectTerminals method. When the terminal list for current input char
             * has more than 1 terminal, the scanner code gets the current parser state from core parser (through compilerContext),
             * and then checks the terminals agains the ExpectedTerms set in the parser state.
             * As you might see in Grammar Explorer, the conflict is resolved successfully.
             */

            //Terminals
            var lineNumber = new NumberLiteral("LINE_NUMBER", NumberOptions.IntOnly);
            var fileNumber = new NumberLiteral("FILE_NUMBER", NumberOptions.IntOnly);

            var number = new NumberLiteral("NUMBER", NumberOptions.AllowStartEndDot);

            //ints that are too long for int32 are converted to int64
            number.DefaultIntTypes = new TypeCode[] { TypeCode.Int32, TypeCode.Int64 };
            number.AddExponentSymbols("eE", TypeCode.Single);
            number.AddExponentSymbols("dD", TypeCode.Double);
            number.AddSuffix("!", TypeCode.Single);
            number.AddSuffix("#", TypeCode.Double);

            var variable = new IdentifierTerminal("Identifier");

            variable.AddSuffix("$", TypeCode.String);
            variable.AddSuffix("%", TypeCode.Int32);
            variable.AddSuffix("!", TypeCode.Single);
            variable.AddSuffix("#", TypeCode.Double);

            var stringLiteral = new StringLiteral("STRING", "\"", StringOptions.None);
            //Important: do not add comment term to base.NonGrammarTerminals list - we do use this terminal in grammar rules
            var userFunctionName = variable;
            var comment          = new CommentTerminal("Comment", "REM", "\n");
            var short_comment    = new CommentTerminal("ShortComment", "'", "\n");
            var line_cont        = new LineContinuationTerminal("LineContinuation", "_");
            var comma            = ToTerm(",", "comma");
            var colon            = ToTerm(":", "colon");

            var comma_opt = new NonTerminal("comma_opt");

            comma_opt.Rule = Empty | ",";
            var semi_opt = new NonTerminal("semi_opt");

            semi_opt.Rule = Empty | ";";
            var pound_opt = new NonTerminal("pound_opt");

            pound_opt.Rule = Empty | "#";

            // Non-terminals
            var PROGRAM                   = new NonTerminal("PROGRAM");
            var LINE                      = new NonTerminal("LINE");
            var LINE_CONTENT_OPT          = new NonTerminal("LINE_CONTENT_OPT");
            var COMMENT_OPT               = new NonTerminal("COMMENT_OPT");
            var STATEMENT_LIST            = new NonTerminal("STATEMENT_LIST");
            var STATEMENT                 = new NonTerminal("STATEMENT");
            var PRINT_STMT                = new NonTerminal("PRINT_STMT");
            var PRINT_LIST                = new NonTerminal("PRINT_LIST");
            var PRINT_ARG                 = new NonTerminal("PRINT_ARG");
            var OPEN_STMT                 = new NonTerminal("OPEN_STMT");
            var OPEN_STMT_MODE            = new NonTerminal("OPEN_STMT_MODE");
            var OPEN_STMT_ACCESS          = new NonTerminal("OPEN_STMT_ACCESS");
            var CLOSE_STMT                = new NonTerminal("CLOSE_STMT");
            var INPUT_STMT                = new NonTerminal("INPUT_STMT");
            var VARIABLES                 = new NonTerminal("VARIABLES");
            var IF_STMT                   = new NonTerminal("IF_STMT");
            var THEN_CLAUSE               = new NonTerminal("THEN_CLAUSE");
            var ELSE_CLAUSE_OPT           = new NonTerminal("ELSE_CLAUSE_OPT"); //, typeof(AstNode));
            var EXPR                      = new NonTerminal("EXPRESSION");
            var EXPR_LIST                 = new NonTerminal("EXPRESSION_LIST");
            var BINARY_OP                 = new NonTerminal("BINARY_OP", "operator");
            var BINARY_EXPR               = new NonTerminal("BINARY_EXPR");
            var UNARY_EXPR                = new NonTerminal("UNARY_EXPR");
            var SIGN                      = new NonTerminal("SIGN");
            var ASSIGN_STMT               = new NonTerminal("ASSIGN_STMT");
            var FOR_STMT                  = new NonTerminal("FOR_STMT");
            var STEP_OPT                  = new NonTerminal("STEP_OPT");
            var NEXT_STMT                 = new NonTerminal("NEXT_STMT");
            var LOCATE_STMT               = new NonTerminal("LOCATE_STMT");
            var WHILE_STMT                = new NonTerminal("WHILE_STMT");
            var WEND_STMT                 = new NonTerminal("WEND_STMT");
            var SWAP_STMT                 = new NonTerminal("SWAP_STMT");
            var FUN_CALL                  = new NonTerminal("FUN_CALL");
            var VARIABLE_OR_FUNCTION_EXPR = new NonTerminal("VARIABLE_OR_FUNCTION_EXPR");
            var ARG_LIST                  = new NonTerminal("ARG_LIST");
            var LINE_INPUT_STMT           = new NonTerminal("LINE_INPUT_STMT");
            var LINE_INPUT_POUND_STMT     = new NonTerminal("LINE_INPUT_POUND_STMT");
            var END_STMT                  = new NonTerminal("END_STMT");
            var CLS_STMT                  = new NonTerminal("CLS_STMT");
            var CLEAR_STMT                = new NonTerminal("CLEAR_STMT");
            var DIM_STMT                  = new NonTerminal("DIM_STMT");
            var DEF_FN_STMT               = new NonTerminal("DEF_FN_STMT");
            var GOTO_STMT                 = new NonTerminal("GOTO_STMT");
            var GOSUB_STMT                = new NonTerminal("GOSUB_STMT");
            var RETURN_STMT               = new NonTerminal("RETURN_STMT");
            var ON_STMT                   = new NonTerminal("ON_STMT");
            var LINE_NUMBERS              = new NonTerminal("LINE_NUMBERS");
            var RANDOMIZE_STMT            = new NonTerminal("RANDOMIZE_STMT");

            // set the PROGRAM to be the root node of BASIC programs.
            this.Root = PROGRAM;

            // BNF Rules
            PROGRAM.Rule = MakePlusRule(PROGRAM, 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 | lineNumber + LINE_CONTENT_OPT + COMMENT_OPT + NewLine | SyntaxError + NewLine;
            LINE.NodeCaptionTemplate = "Line #{0}";

            // A statement list is 1 or more statements separated by the ':' character
            LINE_CONTENT_OPT.Rule = Empty | IF_STMT | STATEMENT_LIST;
            STATEMENT_LIST.Rule   = MakePlusRule(STATEMENT_LIST, colon, STATEMENT);
            COMMENT_OPT.Rule      = short_comment | comment | Empty;

            // A statement can be one of a number of types
            STATEMENT.Rule = ASSIGN_STMT | PRINT_STMT | INPUT_STMT | OPEN_STMT | CLOSE_STMT
                             | LINE_INPUT_POUND_STMT | LINE_INPUT_STMT
                             | LOCATE_STMT | CLS_STMT
                             | END_STMT | CLEAR_STMT | DIM_STMT | DEF_FN_STMT
                             | SWAP_STMT | RANDOMIZE_STMT
                             | GOSUB_STMT | RETURN_STMT | GOTO_STMT | ON_STMT
                             | FOR_STMT | NEXT_STMT | WHILE_STMT | WEND_STMT;

            // The different statements are defined here
            PRINT_STMT.Rule = "print" + PRINT_LIST;
            PRINT_LIST.Rule = MakeStarRule(PRINT_LIST, null, PRINT_ARG);
            PRINT_ARG.Rule  = EXPR + semi_opt;
            INPUT_STMT.Rule = "input" + semi_opt + stringLiteral + ";" + VARIABLES;
            OPEN_STMT.Rule  = "open" + EXPR + (Empty | "for" + OPEN_STMT_MODE) +
                              (Empty | "access" + OPEN_STMT_ACCESS) + "as" + pound_opt + fileNumber;
            OPEN_STMT_ACCESS.Rule      = "read" + (Empty | "write") | "write";
            OPEN_STMT_MODE.Rule        = ToTerm("o") | "i" | "a" | "output" | "input" | "append";
            CLOSE_STMT.Rule            = "close" + pound_opt + fileNumber;
            LINE_INPUT_STMT.Rule       = ToTerm("line") + "input" + semi_opt + stringLiteral + ";" + VARIABLE_OR_FUNCTION_EXPR;
            LINE_INPUT_POUND_STMT.Rule = ToTerm("line") + "input" + ToTerm("#") + fileNumber + comma + VARIABLE_OR_FUNCTION_EXPR;
            DIM_STMT.Rule    = "dim" + VARIABLES;
            DEF_FN_STMT.Rule = "def" + userFunctionName + (Empty | "(" + ARG_LIST + ")") + "=" + EXPR;
            VARIABLES.Rule   = VARIABLE_OR_FUNCTION_EXPR | VARIABLE_OR_FUNCTION_EXPR + "," + VARIABLES;

            IF_STMT.Rule     = "if" + EXPR + THEN_CLAUSE + ELSE_CLAUSE_OPT;
            THEN_CLAUSE.Rule = "then" + STATEMENT_LIST | GOTO_STMT;

            //Inject PreferShift hint here to explicitly set shift as preferred action. Suppresses warning message about conflict.
            ELSE_CLAUSE_OPT.Rule = Empty | PreferShiftHere() + "else" + STATEMENT_LIST;

            GOTO_STMT.Rule      = "goto" + lineNumber;
            GOSUB_STMT.Rule     = "gosub" + lineNumber;
            RETURN_STMT.Rule    = "return";
            ON_STMT.Rule        = "on" + EXPR + (ToTerm("goto") | "gosub") + LINE_NUMBERS;
            LINE_NUMBERS.Rule   = MakePlusRule(LINE_NUMBERS, comma, lineNumber);
            ASSIGN_STMT.Rule    = VARIABLE_OR_FUNCTION_EXPR + "=" + EXPR;
            LOCATE_STMT.Rule    = "locate" + EXPR + comma + EXPR;
            SWAP_STMT.Rule      = "swap" + EXPR + comma + EXPR;
            END_STMT.Rule       = "end";
            CLS_STMT.Rule       = "cls";
            CLEAR_STMT.Rule     = ToTerm("clear") + comma + (Empty | number) + (Empty | comma + number) | "clear" + number | "clear";
            RANDOMIZE_STMT.Rule = "randomize" + EXPR;

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

            //Inject PreferShift hint here to explicitly set shift as preferred action. Suppresses warning message about conflict.
            //The conflict arises from PRINT statement, when there may be ambigous interpretations for expression like
            //  PRINT F (X) -- either function call or identifier followed by expression
            FUN_CALL.Rule = variable + PreferShiftHere() + "(" + ARG_LIST + ")";
            VARIABLE_OR_FUNCTION_EXPR.Rule = variable | FUN_CALL;

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

            EXPR_LIST.Rule = MakeStarRule(EXPR_LIST, EXPR);

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

            //TODO: check number of arguments for particular function in node constructor
            ARG_LIST.Rule = MakePlusRule(ARG_LIST, comma, EXPR);


            //Punctuation and Transient elements
            MarkPunctuation("(", ")", ",");
            MarkTransient(EXPR, STATEMENT, LINE_CONTENT_OPT, VARIABLE_OR_FUNCTION_EXPR, COMMENT_OPT);
            NonGrammarTerminals.Add(line_cont);

            this.LanguageFlags = LanguageFlags.NewLineBeforeEOF;

            lineNumber.ValidateToken += identifier_ValidateToken;
        }
Esempio n. 19
0
        public LiteralRules(PieGrammar grammar)
        {
            this.grammar = grammar;

            NumberLiteral  = new NumberLiteral("number_literal", NumberOptions.AllowStartEndDot)
            {
                // Define types to assign if the parser can't determine.
                DefaultIntTypes = new[] { TypeCode.Int32 },
                DefaultFloatType = TypeCode.Double
            };

            // The pre and post fixes for different number types and formats.
            NumberLiteral.AddPrefix("0x", NumberOptions.Hex);
            NumberLiteral.AddSuffix("b", TypeCode.Byte);
            NumberLiteral.AddSuffix("B", TypeCode.Byte);
            NumberLiteral.AddSuffix("s", TypeCode.Int16);
            NumberLiteral.AddSuffix("S", TypeCode.Int16);
            NumberLiteral.AddSuffix("u", TypeCode.UInt32);
            NumberLiteral.AddSuffix("U", TypeCode.UInt32);
            NumberLiteral.AddSuffix("us", TypeCode.UInt16);
            NumberLiteral.AddSuffix("US", TypeCode.UInt16);
            NumberLiteral.AddSuffix("l", TypeCode.Int64);
            NumberLiteral.AddSuffix("L", TypeCode.Int64);
            NumberLiteral.AddSuffix("ul", TypeCode.UInt64);
            NumberLiteral.AddSuffix("UL", TypeCode.UInt64);
            NumberLiteral.AddSuffix("F", TypeCode.Single);
            NumberLiteral.AddSuffix("f", TypeCode.Single);
            NumberLiteral.AddSuffix("d", TypeCode.Double);
            NumberLiteral.AddSuffix("D", TypeCode.Double);
            NumberLiteral.AddSuffix("m", TypeCode.Decimal);
            NumberLiteral.AddSuffix("M", TypeCode.Decimal);

            StringLiteral = new StringLiteral("string_literal", "\"", StringOptions.AllowsAllEscapes);

            CharLiteral = new StringLiteral("char_literal", "'", StringOptions.IsChar);

            BoolLiteral = new NonTerminal("bool_literal");
            BoolLiteral.Rule = grammar.Keywords.True | grammar.Keywords.False;

            Null = grammar.ToTerm("null", "null_literal");
        }
Esempio n. 20
0
        public RefalGrammar() : base(true)         // case sensitive
        {
            GrammarComments = "Refal-5 language interpreter based on Irony toolkit.";

            // Terminals
            var Number = new NumberLiteral("Number");

            Number.DefaultIntTypes = new TypeCode[] { TypeCode.Int32, TypeCode.Int64, NumberLiteral.TypeCodeBigInt };
            Number.AddPrefix("0x", NumberOptions.Hex);
            Number.AddSuffix("u", TypeCode.UInt32);
            Number.AddSuffix("l", TypeCode.Int64);
            Number.AddSuffix("ul", TypeCode.UInt64);
            Number.Options |= NumberOptions.IntOnly;

            var CharLiteral   = new StringLiteral("Char", "'", StringOptions.AllowsAllEscapes);
            var StringLiteral = new StringLiteral("String", "\"", StringOptions.AllowsAllEscapes);
            var Identifier    = new IdentifierTerminal("Identifier", "_-", "");

            var LineComment = new CommentTerminal("LineComment", "*", "\n", "\r");

            LineComment.ValidateToken += LineComment_ValidateToken;

            var BlockComment = new CommentTerminal("BlockComment", "/*", "*/");

            NonGrammarTerminals.Add(LineComment);
            NonGrammarTerminals.Add(BlockComment);

            // Non-terminals
            var Program                 = new NonTerminal("Program", typeof(Program));
            var Definition              = new NonTerminal("Definition");
            var Function                = new NonTerminal("Function", typeof(DefinedFunction));
            var External                = new NonTerminal("External", typeof(AuxiliaryNode));
            var IdentifierList          = new NonTerminal("Identifier+", typeof(AuxiliaryNode));
            var Block                   = new NonTerminal("Block", typeof(Block));
            var Sentence                = new NonTerminal("Sentence", typeof(Sentence));
            var RSentence               = new NonTerminal("RSentence", typeof(AuxiliaryNode));
            var SentenceList            = new NonTerminal("Sentence+", typeof(AuxiliaryNode));
            var Pattern                 = new NonTerminal("Pattern", typeof(Pattern));
            var PatternItem             = new NonTerminal("PatternItem");
            var PatternInParentheses    = new NonTerminal("(Pattern)", typeof(ExpressionInBraces));
            var Expression              = new NonTerminal("Expression", typeof(Expression));
            var ExpressionItem          = new NonTerminal("ExpressionItem");
            var ExpressionInParentheses = new NonTerminal("(Expression)", typeof(ExpressionInBraces));
            var Var                            = new NonTerminal("Variable", Variable.CreateVariableNode);
            var VarPrefix                      = new NonTerminal("VariablePrefix");
            var VarIndex                       = new NonTerminal("VariableIndex");
            var Symbol                         = new NonTerminal("Symbol", LiteralValueNodeHelper.InitNode);
            var Call                           = new NonTerminal("Call", typeof(FunctionCall));
            var FunctionName                   = new NonTerminal("FunctionName", typeof(AuxiliaryNode));
            var WhereOrWithClause              = new NonTerminal("WhereOrWithClause", typeof(Conditions));
            var CommaOrAmpersand               = new NonTerminal("CommaOrAmpersand");
            var RWhereOrWithClause             = new NonTerminal("RWhereOrWithClause", typeof(AuxiliaryNode));
            var RExpressionOrWhereOrWithClause = new NonTerminal("RExpressionOrWhereOrWithClause");

            var SemicolonOpt = new NonTerminal("[;]", Empty | ";");
            var EntryOpt     = new NonTerminal("[ENTRY]", Empty | "$ENTRY");
            var Extern       = new NonTerminal("Extern", ToTerm("$EXTRN") | "$EXTERN" | "$EXTERNAL");

            // Rules
            Root = Program;

            Program.Rule        = MakePlusRule(Program, Definition);
            Definition.Rule     = Function | External;
            External.Rule       = Extern + IdentifierList + ";";
            IdentifierList.Rule = MakePlusRule(IdentifierList, ToTerm(","), Identifier);

            Function.Rule     = EntryOpt + Identifier + Block + SemicolonOpt;
            Block.Rule        = "{" + SentenceList + SemicolonOpt + "}";
            SentenceList.Rule = MakePlusRule(SentenceList, ToTerm(";"), Sentence);

            Sentence.Rule                = Pattern + RSentence;
            RSentence.Rule               = "=" + Expression | WhereOrWithClause;
            Pattern.Rule                 = MakeStarRule(Pattern, PatternItem);
            PatternItem.Rule             = Var | Symbol | PatternInParentheses;
            PatternInParentheses.Rule    = "(" + Pattern + ")";
            Expression.Rule              = MakeStarRule(Expression, ExpressionItem);
            ExpressionItem.Rule          = Call | Var | Symbol | ExpressionInParentheses;
            ExpressionInParentheses.Rule = "(" + Expression + ")";

            Var.Rule          = VarPrefix + "." + VarIndex;
            VarPrefix.Rule    = ToTerm("e") | "s" | "t";
            VarIndex.Rule     = Number | Identifier;
            Symbol.Rule       = StringLiteral | CharLiteral | Number | "True" | "False" | Identifier;
            Call.Rule         = "<" + FunctionName + Expression + ">";
            FunctionName.Rule = Identifier | "+" | "-" | "*" | "/";

            WhereOrWithClause.Rule  = CommaOrAmpersand + Expression + ":" + RWhereOrWithClause;
            CommaOrAmpersand.Rule   = ToTerm(",") | "&";
            RWhereOrWithClause.Rule = Block                                       // with-clause
                                      | Pattern + RExpressionOrWhereOrWithClause; // where-clause
            RExpressionOrWhereOrWithClause.Rule = ToTerm("=") + Expression | WhereOrWithClause;

            // Punctuation, braces, transient terms, options
            MarkPunctuation("(", ")");
            MarkPunctuation("{", "}");
            MarkPunctuation("<", ">");
            MarkPunctuation("=", ",", "&", ";");

            RegisterBracePair("(", ")");
            RegisterBracePair("<", ">");
            RegisterBracePair("{", "}");

            MarkTransient(Definition, PatternItem, ExpressionItem, SemicolonOpt, EntryOpt, Extern, CommaOrAmpersand, VarPrefix, VarIndex, RExpressionOrWhereOrWithClause);
            LanguageFlags = LanguageFlags.CreateAst | LanguageFlags.SupportsBigInt | LanguageFlags.TailRecursive;
        }
Esempio n. 21
0
        public LuaGrammar()
            : base(true)
        {
            #region Terminals
            Terminal Identifier   = new IdentifierTerminal("identifier");
            Terminal SingleString = new StringLiteral("string", "'", StringOptions.AllowsAllEscapes);
            Terminal DoubleString = new StringLiteral("string", "\"", StringOptions.AllowsAllEscapes);

            var number = new NumberLiteral("number", NumberOptions.AllowSign);
            number.AddPrefix("0x", NumberOptions.Hex);
            number.AddSuffix("LL", TypeCode.Int64);
            Terminal Number = number;

            Terminal LineComment = new CommentTerminal("Comment", "--", "\n", "\r");
            Terminal LongComment = new CommentTerminal("LongComment", "--[[", "]]");

            base.NonGrammarTerminals.Add(LongComment);
            base.NonGrammarTerminals.Add(LineComment);
            #endregion

            #region Nonterminals
            NonTerminal Chunk = new NonTerminal("Chunk");

            NonTerminal Prefix                = new NonTerminal("Prefix");
            NonTerminal Variable              = new NonTerminal("Variable");
            NonTerminal FunctionCall          = new NonTerminal("FunctionCall");
            NonTerminal CallArguments         = new NonTerminal("FunctionCallArguments");
            NonTerminal OopCall               = new NonTerminal("OopCall");
            NonTerminal CallArgumentsFragment = new NonTerminal("");
            NonTerminal Expression            = new NonTerminal("Expression");
            NonTerminal FunctionDef           = new NonTerminal("FunctionDef");
            NonTerminal DefArguments          = new NonTerminal("FunctionDefArguments");
            NonTerminal DefArgumentsFragment  = new NonTerminal("");

            NonTerminal Statement       = new NonTerminal("Statement");
            NonTerminal ReturnStatement = new NonTerminal("ReturnStat");
            NonTerminal BreakStatement  = new NonTerminal("BreakStat");
            NonTerminal Assignment      = new NonTerminal("Assignment");
            NonTerminal LocalAssignment = new NonTerminal("LocalAssignment");
            NonTerminal FunctionDecl    = new NonTerminal("FunctionDecl");
            NonTerminal DoBlock         = new NonTerminal("DoBlock");
            NonTerminal If     = new NonTerminal("If");
            NonTerminal Elseif = new NonTerminal("Elseif");
            NonTerminal Else   = new NonTerminal("Else");
            NonTerminal While  = new NonTerminal("While");
            NonTerminal Repeat = new NonTerminal("Repeat");
            NonTerminal For    = new NonTerminal("For");

            NonTerminal PowerOp   = new NonTerminal("PowerOp");
            NonTerminal MulOp     = new NonTerminal("MulOp");
            NonTerminal AddOp     = new NonTerminal("AddOp");
            NonTerminal ConcatOp  = new NonTerminal("ConcatOp");
            NonTerminal RelOp     = new NonTerminal("RelOp");
            NonTerminal AndOp     = new NonTerminal("AndOp");
            NonTerminal OrOp      = new NonTerminal("OrOp");
            NonTerminal UnaryExpr = new NonTerminal("UnaryExpr");

            NonTerminal TableConstruct         = new NonTerminal("TableConstruct");
            NonTerminal TableConstructFragment = new NonTerminal("TableConstructFragment");

            NonTerminal Varargs = new NonTerminal("Varargs");
            #endregion

            #region Fragments
            Varargs.Rule = "...";

            CallArgumentsFragment.Rule = Expression | Expression + "," + CallArgumentsFragment;

            CallArguments.Rule = "(" + (CallArgumentsFragment | Empty) + ")";

            DefArgumentsFragment.Rule = (Varargs | Identifier) | (Varargs | Identifier) + "," + DefArgumentsFragment;

            DefArguments.Rule = "(" + (DefArgumentsFragment | Empty) + ")";

            Chunk.Rule = MakeStarRule(Chunk, Statement);
            #endregion

            #region Expressions
            PowerOp.Rule  = Expression + ("^" + Expression | Empty);
            MulOp.Rule    = PowerOp + ((ToTerm("*") | "/" | "%") + PowerOp | Empty);
            AddOp.Rule    = MulOp + ((ToTerm("+") | "-") + MulOp | Empty);
            ConcatOp.Rule = AddOp + (".." + AddOp | Empty);
            RelOp.Rule    = ConcatOp + ((ToTerm(">") | ">=" | "<" | "<=" | "==" | "~=") + ConcatOp | Empty);
            AndOp.Rule    = RelOp + ("and" + RelOp | Empty);
            OrOp.Rule     = AndOp + ("or" + AndOp | Empty);

            UnaryExpr.Rule = (ToTerm("not") | "-" | "#") + Expression;

            Prefix.Rule =
                OopCall
                | FunctionCall
                | Variable
                | "(" + Expression + ")";

            Variable.Rule =
                Prefix + "." + Identifier
                | Prefix + "[" + Expression + "]"
                | Identifier;

            FunctionCall.Rule = Prefix + CallArguments;
            OopCall.Rule      = Prefix + ":" + Identifier + CallArguments;

            FunctionDef.Rule =
                ToTerm("function") + DefArguments
                + Chunk + ToTerm("end");

            var tableSep = new NonTerminal("");
            tableSep.Rule = ToTerm(";") | ",";
            TableConstructFragment.Rule =
                (
                    (
                        (
                            (Identifier | "[" + Expression + "]") + "="
                        )
                        + Expression
                        | Expression
                    )
                    + (";" + TableConstructFragment | "," + TableConstructFragment | Empty)
                ) | Empty;
            TableConstruct.Rule = "{" + TableConstructFragment + "}";

            Expression.Rule =
                Varargs
                | Prefix
                | OrOp
                | UnaryExpr
                | ToTerm("true")
                | "false"
                | "nil"
                | SingleString
                | DoubleString
                | Number
                | FunctionDef
                | TableConstruct;
            #endregion

            #region Statements
            FunctionDecl.Rule = "function" + Variable + (":" + Identifier | Empty) + DefArguments + Chunk + "end";


            var RetChunk = new NonTerminal("RetChunk");
            RetChunk.Rule = Expression + (("," + RetChunk) | Empty);

            ReturnStatement.Rule =
                "return" + (RetChunk | Empty);

            var AssignExpChunk = new NonTerminal("AssignExpChunk");
            AssignExpChunk.Rule = Expression + (("," + AssignExpChunk) | Empty);
            var AssignVarChunk = new NonTerminal("AssignVarChunk");
            AssignVarChunk.Rule = Variable + (("," + AssignVarChunk) | Empty);

            Assignment.Rule =
                AssignVarChunk + "=" + AssignExpChunk;

            var LocalAssignNameChunk = new NonTerminal("AssignNameChunk");
            var LocalFunction        = new NonTerminal("LocalFunction");
            LocalAssignNameChunk.Rule = Identifier + (("," + LocalAssignNameChunk) | Empty);
            LocalFunction.Rule        = "function" + Identifier + DefArguments + Chunk + "end";
            LocalAssignment.Rule      = "local" + (LocalAssignNameChunk + ("=" + AssignExpChunk | Empty) | LocalFunction);

            Elseif.Rule = "elseif" + Expression + "then" + Chunk + (Elseif | Empty);
            Else.Rule   = "else" + Chunk;

            If.Rule = "if" + Expression + "then" + Chunk + (Elseif | Empty) + (Else | Empty) + "end";

            While.Rule = "while" + Expression + DoBlock;

            DoBlock.Rule = "do" + Chunk + "end";

            Repeat.Rule = "repeat" + Chunk + "until" + Expression;

            BreakStatement.Rule = "break";

            var NumericFor = new NonTerminal("NumericFor");
            NumericFor.Rule = Identifier + "=" + Expression + "," + Expression + ("," + Expression | Empty);
            var GenericFor = new NonTerminal("GenericFor");
            var NameList   = new NonTerminal("NameList");
            var ExpList    = new NonTerminal("ExpList");
            NameList.Rule   = Identifier + (("," + NameList) | Empty);
            ExpList.Rule    = Expression + (("," + ExpList) | Empty);
            GenericFor.Rule = NameList + "in" + ExpList;

            For.Rule = "for" + (GenericFor | NumericFor) + DoBlock;

            Statement.Rule =
                ";"
                | ReturnStatement
                | BreakStatement
                | Assignment
                | LocalAssignment
                | FunctionCall
                | OopCall
                | FunctionDecl
                | For
                | If
                | While
                | DoBlock
                | Repeat;
            #endregion

            this.Root = Chunk;
            this.MarkReservedWords(
                "true", "false",
                "nil", "local",
                "function", "while",
                "if", "for", "repeat", "until",
                "end", "do", "return", "break");

            this.MarkPunctuation(".", ",", ";", "(", ")", "[", "]", "{", "}", "=", ":");
            this.MarkTransient(Statement);
        }