Example #1
0
 private Token peek(int offset = 0)
 {
     if (i <= tokens.Count)
     {
         try
         {
             return(tokens[i + offset]);
         }
         catch { }
         Tokens.Statement stat = new Tokens.Statement();
         stat.Name = "";
         return(stat);
     }
     else
     {
         Tokens.Statement stat = new Tokens.Statement();
         stat.Name = "";
         return(stat);
     }
 }
Example #2
0
 private Token read()
 {
     if (i <= tokens.Count)
     {
         try
         {
             return(tokens[i++]);
         }
         catch { }
         Tokens.Statement stat = new Tokens.Statement();
         stat.Name = "";
         return(stat);
     }
     else
     {
         Tokens.Statement stat = new Tokens.Statement();
         stat.Name = "";
         return(stat);
     }
 }
Example #3
0
        private void doScan()
        {
            char val = (char)(byte)read();

            while (peek() != -1)
            {
                while (char.IsWhiteSpace(val) || val == '\n')
                {
                    if (val == '\n' || val == '\r' || val == Environment.NewLine[0])
                    {
                        if (ScanWhiteSpaces)
                        {
                            addToken(new Tokens.EOL());
                        }
                        this.lineNumber++;
                    }
                    val = (char)(byte)read();
                }
                if (peek() == -1)
                {
                    break;
                }
                if (char.IsLetter(val))
                {
                    StringBuilder sb = new StringBuilder();
                    while (char.IsLetterOrDigit(val) || val == '_')
                    {
                        sb.Append(val);
                        val = (char)(byte)read();
                    }
                    Tokens.Statement stmt = new Tokens.Statement();
                    stmt.Name = sb.ToString();

                    addToken(stmt);
                }
                else if (val == '/' && (char)(byte)peek() == '/')
                {
                    while (val != '\n' && val != '\r')
                    {
                        val = (char)(byte)read();
                    }
                }
                else if (val == '0' && (char)(byte)peek() == 'x')
                {
                    read();
                    val = (char)(byte)read();
                    StringBuilder sb = new StringBuilder();
                    while (char.IsDigit(val) || val.ToString().ToLower() == "a" || val.ToString().ToLower() == "b" || val.ToString().ToLower() == "c" || val.ToString().ToLower() == "d" || val.ToString().ToLower() == "e" || val.ToString().ToLower() == "f")
                    {
                        sb.Append(val);
                        val = (char)(byte)read();
                    }
                    Tokens.IntLiteral il = new Tokens.IntLiteral();
                    il.Value = Int32.Parse(sb.ToString(), System.Globalization.NumberStyles.HexNumber);
                    addToken(il);
                }
                else if (val == '\'')
                {
                    Tokens.IntLiteral il = new Tokens.IntLiteral();
                    bool b = false;
                    if (((char)peek() == '\\'))
                    {
                        read();
                        if (((char)read()).ToString().ToLower()[0] == 'n')
                        {
                            il.Value = 10;
                        }
                        else if (((char)read()).ToString().ToLower()[0] == 'a')
                        {
                            il.Value = 7;
                        }
                        else if (((char)read()).ToString().ToLower()[0] == 'b')
                        {
                            il.Value = 8;
                        }
                        else if (((char)read()).ToString().ToLower()[0] == 'f')
                        {
                            il.Value = 12;
                        }
                        else if (((char)read()).ToString().ToLower()[0] == 'r')
                        {
                            il.Value = 13;
                        }
                        else if (((char)read()).ToString().ToLower()[0] == 't')
                        {
                            il.Value = 9;
                        }
                        else if (((char)read()).ToString().ToLower()[0] == 'v')
                        {
                            il.Value = 11;
                        }
                        else if (((char)read()).ToString().ToLower()[0] == '\'')
                        {
                            il.Value = 44;
                        }
                        else if (((char)read()).ToString().ToLower()[0] == '\"')
                        {
                            il.Value = 34;
                        }
                        b = true;
                    }
                    else
                    {
                        il.Value = read();
                        if (((char)read()) != '\'')
                        {
                            throw new Exception("Expected '");
                        }
                    }
                    if (b)
                    {
                        read();
                    }
                    b   = false;
                    val = ((char)read());
                    this.tokens.Add(il);
                }
                else if (char.IsDigit(val) || (val == '-' && char.IsDigit((char)(byte)peek())))
                {
                    StringBuilder sb = new StringBuilder();
                    while (char.IsDigit(val) || val == '.' || val == '-')
                    {
                        sb.Append(val);
                        val = (char)(byte)read();
                    }
                    if (sb.ToString().Contains(".") || ((char)peek()) == 'f')
                    {
                        if (((char)peek()) == 'f')
                        {
                            val = (char)(byte)read();
                        }
                        Tokens.FloatLiteral fl = new Tokens.FloatLiteral();
                        fl.Value = float.Parse(sb.ToString());
                        addToken(fl);
                    }
                    else
                    {
                        Tokens.IntLiteral il = new Tokens.IntLiteral();
                        il.Value = Convert.ToInt32(sb.ToString());
                        addToken(il);
                    }
                }

                else if (val == '"')
                {
                    StringBuilder sb = new StringBuilder();
                    val = (char)(byte)read();
                    while (val != '"')
                    {
                        if (val == '\\')
                        {
                            char escape = (char)(byte)read();
                            if (escape == 'n')
                            {
                                sb.Append('\n');
                            }
                            else if (escape == '"')
                            {
                                sb.Append('"');
                            }
                            else if (escape == '\\')
                            {
                                sb.Append('\\');
                            }
                            else if (escape == '\'')
                            {
                                sb.Append('\'');
                            }
                            else if (escape == '0')
                            {
                                sb.Append('\0');
                            }
                            else if (escape == 'r')
                            {
                                sb.Append('\r');
                            }
                        }
                        else
                        {
                            sb.Append(val);
                        }
                        val = (char)(byte)read();
                    }
                    val = (char)(byte)read();
                    Tokens.StringLiteral sl = new Tokens.StringLiteral();
                    sl.Value = sb.ToString();
                    addToken(sl);
                }
                else
                {
                    switch (val)
                    {
                    case '+':
                        if ((char)(byte)peek() == '+')
                        {
                            read();
                            addToken(new Tokens.Increment());
                        }
                        else if ((char)(byte)peek() == '=')
                        {
                            read();
                            addToken(new Tokens.PlusEquals());
                        }
                        else
                        {
                            addToken(new Tokens.Add());
                        }
                        break;

                    case '-':
                        if ((char)(byte)peek() == '-')
                        {
                            read();
                            addToken(new Tokens.Increment());
                        }
                        else if ((char)(byte)peek() == '>')
                        {
                            read();
                            addToken(new Tokens.ObjectReference());
                        }
                        else
                        {
                            addToken(new Tokens.Sub());
                        }
                        break;

                    case '|':
                        if (((char)peek()) == '|')
                        {
                            read();
                            addToken(new Tokens.BooleanOr());
                        }
                        else
                        {
                            addToken(new Tokens.Pipe());
                        }
                        break;

                    case '^':
                        if ((char)(byte)peek() == '=')
                        {
                            read();
                            addToken(new Tokens.PowEqual());
                        }
                        else
                        {
                            addToken(new Tokens.Pow());
                        };
                        break;

                    case '%':
                        addToken(new Tokens.Percent());
                        break;

                    case '?':
                        addToken(new Tokens.Question());
                        break;

                    case '/':
                        addToken(new Tokens.Div());
                        break;

                    case '*':
                        addToken(new Tokens.Mul());
                        break;

                    case ',':
                        addToken(new Tokens.Comma());
                        break;

                    case '.':
                        addToken(new Tokens.Dot());
                        break;

                    case ':':
                        addToken(new Tokens.Colon());
                        break;

                    case '#':
                        addToken(new Tokens.Hash());
                        break;

                    case '@':
                        addToken(new Tokens.At());
                        break;

                    case '>':
                        if ((char)(byte)peek() == '>')
                        {
                            read();
                            addToken(new Tokens.RightBitshift());
                        }
                        else
                        {
                            addToken(new Tokens.GreaterThan());
                        }
                        break;

                    case '<':
                        if ((char)(byte)peek() == '<')
                        {
                            read();
                            addToken(new Tokens.LeftBitshift());
                        }
                        else
                        {
                            addToken(new Tokens.LessThan());
                        }
                        break;

                    case ';':
                        addToken(new Tokens.SemiColon());
                        break;

                    case '{':
                        addToken(new Tokens.openCurlyBracket());
                        break;

                    case '}':
                        addToken(new Tokens.closeCurlyBracket());
                        break;

                    case '~':
                        addToken(new Tokens.Tilda());
                        break;

                    case '(':
                        addToken(new Tokens.openParenthesis());
                        break;

                    case ')':
                        addToken(new Tokens.closeParenthesis());
                        break;

                    case '[':
                        addToken(new Tokens.openBracket());
                        break;

                    case ']':
                        addToken(new Tokens.closeBracket());
                        break;

                    case '&':
                        if (char.IsLetter(((char)peek())))
                        {
                            string accum = "";
                            while (char.IsLetterOrDigit(((char)peek())) || ((char)peek()) == '_')
                            {
                                accum += ((char)read()).ToString();
                            }
                            Tokens.Dereference def = new Tokens.Dereference();
                            def.Name = accum;
                            addToken(def);
                        }
                        else if ((char)(byte)peek() == '&')
                        {
                            read();
                            addToken(new Tokens.BooleanAnd());
                        }
                        else
                        {
                            addToken(new Tokens.And());
                        }
                        break;

                    case '!':
                        if ((char)(byte)peek() == '=')
                        {
                            read();
                            addToken(new Tokens.NotEqual());
                        }
                        else
                        {
                            addToken(new Tokens.Exclamation());
                        }
                        break;

                    case '=':
                        if (!((char)(byte)peek() == '='))
                        {
                            addToken(new Tokens.Assign());
                        }
                        else
                        {
                            read();
                            addToken(new Tokens.Equal());
                        }
                        break;

                    case '_':
                        addToken(new Tokens.Underline());
                        break;

                    case '$':
                        addToken(new Tokens.Dollah());
                        break;

                    default:
                        throw new Exception("Lexer encountered an unexepected character '" + val + "'");
                    }
                    if (peek() != -1)
                    {
                        val = (char)(byte)read();
                    }
                }
            }
        }