Ejemplo n.º 1
0
 public static bool CheckBounds(Token[] tokens, int i)
 {
     if (i >= tokens.Length)
     {
         throw new ParserException("Unexpected end of stream.", i, tokens[i]);
     }
     return true;
 }
Ejemplo n.º 2
0
 public static Token Expect(Token[] tokens, ref int i, TokenType type)
 {
     if (tokens[i].Type != type) //Throw exception if token type does not match expected
     {
         throw new ParserException(string.Format("Expected token of type {0}.", type.ToString()), i, tokens[i]);
     }
     else //Consume token
     {
         Token t = tokens[i];
         i++;
         return t;
     }
 }
Ejemplo n.º 3
0
 static Token ProcessMinus(string raw, ref int i)
 {
     if (raw.Length > i + 1 && raw[i + 1] == '-') //--
     {
         Token t = new Token(raw.Substring(i, 2), TokenType.DoubleMinus);
         i += 2;
         return t;
     }
     else if (raw.Length > i + 1 && raw[i + 1] == '=') //-=
     {
         Token t = new Token(raw.Substring(i, 2), TokenType.MinusEquals);
         i += 2;
         return t;
     }
     else if (raw.Length > i + 1 && raw[i + 1] == '>') //->
     {
         Token t = new Token(raw.Substring(i, 2), TokenType.Arrow);
         i += 2;
         return t;
     }
     else //-
     {
         Token t = new Token(raw[i].ToString(), TokenType.Minus);
         i += 1;
         return t;
     }
 }
Ejemplo n.º 4
0
 static Token ProcessLess(string raw, ref int i)
 {
     if (raw.Length > i + 1 && raw[i + 1] == '=') //Token is '<='
     {
         Token t = new Token(raw.Substring(i, 2), TokenType.LessThanEqual);
         i += 2;
         return t;
     }
     else if (raw.Length > i + 2 && raw.Substring(i, 3) == "<<=") //<<=
     {
         Token t = new Token(raw.Substring(i, 3), TokenType.ShiftLeftEqual);
         i += 3;
         return t;
     }
     else if (raw.Length > i + 1 && raw.Substring(i, 2) == "<<")//<<
     {
         Token t = new Token(raw.Substring(i, 2), TokenType.ShiftLeft);
         i += 2;
         return t;
     }
     else //Token is '<'
     {
         Token t = new Token(raw[i].ToString(), TokenType.LessThan);
         i += 1;
         return t;
     }
 }
Ejemplo n.º 5
0
 static Token ProcessGreater(string raw, ref int i)
 {
     if (raw.Length > i + 1 && raw[i + 1] == '=') //Token is '>='
     {
         Token t = new Token(raw.Substring(i, 2), TokenType.GreaterThanEqual);
         i += 2;
         return t;
     }
     else if (raw.Length > i + 2 && raw.Substring(i, 3) == ">>=") //>>=
     {
         Token t = new Token(raw.Substring(i, 3), TokenType.ShiftRightEqual);
         i += 3;
         return t;
     }
     else if (raw.Length > i + 1 && raw.Substring(i, 2) == ">>")//>>
     {
         Token t = new Token(raw.Substring(i, 2), TokenType.ShiftRight);
         i += 2;
         return t;
     }
     else //Token is '>'
     {
         Token t = new Token(raw[i].ToString(), TokenType.GreaterThan);
         i += 1;
         return t;
     }
 }
Ejemplo n.º 6
0
 static Token ProcessExclamation(string raw, ref int i)
 {
     if (raw.Length > i + 1 && raw[i + 1] == '=') //Token is '!='
     {
         Token t = new Token(raw.Substring(i, 2), TokenType.NotEqual);
         i += 2;
         return t;
     }
     else //Token is '!'
     {
         Token t = new Token(raw[i].ToString(), TokenType.Exclamation);
         i += 1;
         return t;
     }
 }
Ejemplo n.º 7
0
 public static bool Check(Token[] tokens, int i, TokenType type)
 {
     return tokens[i].Type == type;
 }
Ejemplo n.º 8
0
 static Token ProcessAmpersand(string raw, ref int i)
 {
     if (raw.Length > i + 1 && raw[i + 1] == '=') //&=
     {
         Token t = new Token(raw.Substring(i, 2), TokenType.AmpersandEqual);
         i += 2;
         return t;
     }
     else if (raw.Length > i + 1 && raw[i + 1] == '&') //&&
     {
         Token t = new Token(raw.Substring(i, 2), TokenType.DoubleAmpersand);
         i += 2;
         return t;
     }
     else //&
     {
         Token t = new Token(raw[i].ToString(), TokenType.Ampersand);
         i += 1;
         return t;
     }
 }
Ejemplo n.º 9
0
 static Token ProcessStar(string raw, ref int i)
 {
     if (raw.Length > i + 1 && raw[i + 1] == '=') //*=
     {
         Token t = new Token(raw.Substring(i, 2), TokenType.StarEqual);
         i += 2;
         return t;
     }
     else //*
     {
         Token t = new Token(raw[i].ToString(), TokenType.Star);
         i += 1;
         return t;
     }
 }
Ejemplo n.º 10
0
 static Token ProcessCaret(string raw, ref int i)
 {
     if (raw.Length > i + 1 && raw[i + 1] == '=') //^=
     {
         Token t = new Token(raw.Substring(i, 2), TokenType.CaretEqual);
         i += 2;
         return t;
     }
     else //^
     {
         Token t = new Token(raw[i].ToString(), TokenType.Caret);
         i += 1;
         return t;
     }
 }
Ejemplo n.º 11
0
 internal static Token Peek(Token[] tokens, int i)
 {
     return tokens[i];
 }
Ejemplo n.º 12
0
 public static bool CheckBoundsNoThrow(Token[] tokens, int i)
 {
     return (i < tokens.Length);
 }
Ejemplo n.º 13
0
 public static bool CheckLiteral(Token[] tokens, int i, TokenType type, IEnumerable<string> literals)
 {
     return (tokens[i].Type == type) && literals.Contains(tokens[i].Literal);
 }
Ejemplo n.º 14
0
 public static bool CheckLiteral(Token[] tokens, int i, TokenType type, string literal)
 {
     return (tokens[i].Type == type) && (tokens[i].Literal == literal);
 }
Ejemplo n.º 15
0
 static Token ProcessPipe(string raw, ref int i)
 {
     if (raw.Length > i + 1 && raw[i + 1] == '|') //||
     {
         Token t = new Token(raw.Substring(i, 2), TokenType.DoublePipe);
         i += 2;
         return t;
     }
     else if (raw.Length > i + 1 && raw[i + 1] == '=') //|=
     {
         Token t = new Token(raw.Substring(i, 2), TokenType.PipeEqual);
         i += 2;
         return t;
     }
     else //|
     {
         Token t = new Token(raw[i].ToString(), TokenType.Pipe);
         i += 1;
         return t;
     }
 }
Ejemplo n.º 16
0
 static Token ProcessDivide(string raw, ref int i)
 {
     if (raw.Length > i + 1 && raw[i + 1] == '=') // /=
     {
         Token t = new Token(raw.Substring(i, 2), TokenType.DivideEqual);
         i += 2;
         return t;
     }
     else // /
     {
         Token t = new Token(raw[i].ToString(), TokenType.Divide);
         i += 1;
         return t;
     }
 }
Ejemplo n.º 17
0
 static Token ProcessPlus(string raw, ref int i)
 {
     if (raw.Length > i + 1 && raw[i + 1] == '+') //++
     {
         Token t = new Token(raw.Substring(i, 2), TokenType.DoublePlus);
         i += 2;
         return t;
     }
     else if (raw.Length > i + 1 && raw[i + 1] == '=') //+=
     {
         Token t = new Token(raw.Substring(i, 2), TokenType.PlusEqual);
         i += 2;
         return t;
     }
     else //+
     {
         Token t = new Token(raw[i].ToString(), TokenType.Plus);
         i += 1;
         return t;
     }
 }
Ejemplo n.º 18
0
 static Token ProcessDot(string raw, ref int i)
 {
     if (raw.Length > i + 1 && char.IsNumber(raw[i + 1])) //Float literal
     {
         int literalStart = i;
         i += 1; //Skip over '.'
         for (; i < raw.Length && char.IsNumber(raw[i]); i++) ;
         return new Token(raw.Substring(literalStart, i - literalStart), TokenType.Float);
     }
     else //Dot
     {
         Token t = new Token(raw[i].ToString(), TokenType.Dot);
         i += 1;
         return t;
     }
 }
Ejemplo n.º 19
0
 static Token ProcessTilde(string raw, ref int i)
 {
     Token t = new Token(raw[i].ToString(), TokenType.Tilde);
     i += 1;
     return t;
 }
Ejemplo n.º 20
0
 static Token ProcessEqual(string raw, ref int i)
 {
     if (raw.Length > i + 1 && raw[i + 1] == '=') //Token is '=='
     {
         Token t = new Token(raw.Substring(i, 2), TokenType.DoubleEqual);
         i += 2;
         return t;
     }
     else //Token is '='
     {
         Token t = new Token(raw[i].ToString(), TokenType.Equal);
         i += 1;
         return t;
     }
 }
Ejemplo n.º 21
0
 public TokenStream(Token[] tokens)
 {
     _index = 0;
     _tokens = tokens;
 }
Ejemplo n.º 22
0
 public ParserException(string message, int tokenIndex, Token token) : base(message)
 {
     TokenIndex = tokenIndex;
     Token = token;
 }