示例#1
0
        public Lexicon[] Lexiconize(string s)
        {
            text          = s;
            lexicons      = new List <Lexicon>();
            errorProvider = new LexiconizationErrorProvider(s);

            while (true)
            {
                SkipWhiteSpaces();
                if (pos == text.Length)
                {
                    break;
                }


                Lexicon lexicon;

                char c = C();

                if (c >= '0' && c <= '9')
                {
                    lexicon = ReadNumber();
                }
                else if (rule.CharEnclosers.Contains(c))
                {
                    lexicon = ReadChar(c);
                }
                else if (rule.StringEnclosers.Contains(c))
                {
                    lexicon = ReadString(c);
                }
                else if (c >= 'a' && c <= 'z' || c >= 'A' && c <= 'Z' || c == '_')
                {
                    lexicon = ReadWord();
                }
                else
                {
                    lexicon = ReadPunctuation();
                }

                lexicons.Add(lexicon);
            }


            return(lexicons.ToArray());
        }
示例#2
0
 private static int hexCharToInt(char c, int pos, LexiconizationErrorProvider errorProvider)
 {
     if (c >= '0' && c <= '9')
     {
         return(c - '0');
     }
     else if (c >= 'a' && c <= 'f')
     {
         return(c - 'a');
     }
     else if (c >= 'A' && c <= 'F')
     {
         return(c - 'A');
     }
     else
     {
         errorProvider.ThrowException("Expect hex char.", pos);
         return(0);
     }
 }
示例#3
0
            public override char ReadEscapedChar(string s, int startPosition, int[] endPosition, LexiconizationErrorProvider errorProvider)
            {
                char result = Convert.ToChar(200);
                char c      = s[startPosition];

                if (c == '0')
                {
                    result = Convert.ToChar(0);
                }
                else if (c == '\'')
                {
                    result = '\'';
                }
                else if (c == '\"')
                {
                    result = '\"';
                }
                else if (c == '\\')
                {
                    result = '\\';
                }
                else if (c == 'n')
                {
                    result = '\n';
                }
                else if (c == 'r')
                {
                    result = '\r';
                }
                else if (c == 'v')
                {
                    result = Convert.ToChar(11);
                }
                else if (c == 't')
                {
                    result = '\t';
                }
                else if (c == 'b')
                {
                    result = '\b';
                }
                else if (c == 'f')
                {
                    result = '\f';
                }

                if (result != 200)
                {
                    endPosition[0] = startPosition + 1;
                    return(result);
                }


                if (c == 'x')
                {
                    endPosition[0] = startPosition + 3;
                    int d1 = hexCharToInt(s[startPosition + 1], startPosition + 1, errorProvider);
                    int d2 = hexCharToInt(s[startPosition + 2], startPosition + 2, errorProvider);
                    return((char)(d2 + 16 * d1));
                }
                else if (c == 'u')
                {
                    endPosition[0] = startPosition + 5;
                    int d1 = hexCharToInt(s[startPosition + 1], startPosition + 1, errorProvider);
                    int d2 = hexCharToInt(s[startPosition + 2], startPosition + 2, errorProvider);
                    int d3 = hexCharToInt(s[startPosition + 3], startPosition + 3, errorProvider);
                    int d4 = hexCharToInt(s[startPosition + 4], startPosition + 4, errorProvider);
                    return((char)(d4 + 16 * d3 + 16 * 16 * d2 + 16 * 16 * 16 * d1));
                }
                else
                {
                    errorProvider.ThrowException("Unregonized escape char.", startPosition);
                    return(Convert.ToChar(0));
                }
            }