コード例 #1
0
 public void Setup()
 {
     Lexeresults    = new List <KeyValuePair <string, object> >();
     LexerFramework = new HLlangLexerFramework(3);
     //Group 0 for normal Lex
     LexerFramework.AddLexItem("Null", @"\s+", x => null);
     LexerFramework.AddResWords("Type", null, 0, "int", "long", "float", "double", "string", "char", "bool");
     LexerFramework.AddResWords("ResWord", null, 0, "struct", "class", "void", "public", "private");
     LexerFramework.AddDelimiters("Annotation", null, 0, "//", @"/\*");
     LexerFramework.AddOperators("Operator", null, 0, "==", "!=", ">=", "<=", "&&", @"\|\|", @"\*\*", "=", "!", "&", @"\|", "<", ">", @"\*", @"\+", "-", "/", @"\\", "%");
     LexerFramework.AddDelimiters("Delimiter", null, 0, ";", ":", @"\.", ",", "{", "}");
     LexerFramework.AddConstants("String", null, 0, true, "(?=\").*(?=\")");
     LexerFramework.AddConstants("AtString", null, 0, true, "(?=@\").*(?=\")");
     LexerFramework.AddConstants("Char", null, 0, true, "'.'");
     LexerFramework.AddConstants("Real", x => double.Parse(x), 0, false, @"(-|\+?)\d+\.\d+");
     LexerFramework.AddConstants("Digit", x => long.Parse(x), 0, false, @"(-|\+?)[0-9]+");
     LexerFramework.AddConstants("Bool", x => bool.Parse(x), 0, true, "true|false");
     LexerFramework.AddIdentifier();
     //Group 1 for single line annotation
     LexerFramework.CurrentAddGroupNumber = 1;
     LexerFramework.AddLexItem("AnnotationContext", @".+");
     //Group 2 for multiple line annotation
     LexerFramework.CurrentAddGroupNumber = 2;
     LexerFramework.AddDelimiters("Annotation", null, 0, @".*\*/");
     LexerFramework.AddLexItem("AnnotationContext", @".+$", x => null);
     //Bound Event
     LexerFramework.OnLexedEventHandler += OnLexed;
 }
コード例 #2
0
        void SetUpLexer()
        {
            // initialize the results collector
            Lexeresults = new List <KeyValuePair <string, object> >();
            // initialize LexerFramework with three lex groups
            LexerFramework = new HLlangLexerFramework(3);

            // Group 0 for normal Lex

            // drop all blank words
            LexerFramework.AddLexItem("Null", @"\s+", x => null);

            // add Reserved Words first, to avoid be cover by others in matching
            // to distinguish the type reserved words
            LexerFramework.AddResWords("Type", null, 0, "int", "long", "float", "double", "string", "char", "bool");
            // you can also distinguish other reserved words or not
            LexerFramework.AddResWords("ResWord", null, 0, "struct", "class", "void", "public", "private");

            // add all kinds of symbols
            // composite symbol fisrt, such as "//",">=" .etc
            LexerFramework.AddDelimiters("Annotation", null, 0, "//", @"/\*");
            // be careful of Regular Expression Symbols and transfer its meaning
            LexerFramework.AddOperators("Operator", null, 0, "==", "!=", ">=", "<=", "&&", @"\|\|", @"\*\*", "=", "!", "&", @"\|", "<", ">", @"\*", @"\+", "-", "/", @"\\", "%");
            // rest of symbols
            LexerFramework.AddDelimiters("Delimiter", null, 0, ";", ":", @"\.", ",", "{", "}");

            // add all kinds of Constants
            // normal string
            LexerFramework.AddConstants("String", null, 0, true, "(?=\").*(?=\")");
            // un-transferred-meaning string
            LexerFramework.AddConstants("AtString", null, 0, true, "(?=@\").*(?=\")");
            // char
            LexerFramework.AddConstants("Char", null, 0, true, "'.'");
            // double type number
            LexerFramework.AddConstants("Real", x => double.Parse(x), 0, false, @"(-|\+?)\d+\.\d+");
            // int/long type number
            LexerFramework.AddConstants("Digit", x => long.Parse(x), 0, false, @"(-|\+?)[0-9]+");
            // boolean type
            LexerFramework.AddConstants("Bool", x => bool.Parse(x), 0, true, "true|false");

            // add indentifier define (start with [a-zA-Z] and follow [a-zA-Z0-9_]* as normal)
            LexerFramework.AddIdentifier();

            // Group 1 for single line annotation

            // set which group you want to add
            LexerFramework.CurrentAddGroupNumber = 1;
            // add single line annotation context lex item
            LexerFramework.AddLexItem("AnnotationContext", @".+");

            // Group 2 for multiple line annotation

            // set which group you want to add
            LexerFramework.CurrentAddGroupNumber = 2;
            // add multiple line annotation end-word lex item
            LexerFramework.AddDelimiters("Annotation", null, 0, @".*\*/");
            // add multiple line annotation context lex item
            LexerFramework.AddLexItem("AnnotationContext", @".+$", x => null);

            // Bound OnLexed Event
            LexerFramework.OnLexedEventHandler += OnLexed;
        }