public override LuaStatement Parse(INextAwareEnumerator<Token> reader, IParserContext context)
        {
            reader.VerifyExpectedTokenAndMoveNext(Keywords.Function);
            if (reader.Next.Value == LuaToken.Colon)
            {
                reader.MoveNext();
                reader.MoveNext();
            }
            var functionName = reader.GetAndMoveNext();
            reader.VerifyExpectedToken(LuaToken.LeftBracket);

            var argumentNames = new List<string>();
            reader.MoveNext();
            while (reader.Current.Value != LuaToken.RightBracket)
            {
                argumentNames.Add(reader.Current.Value);
                reader.MoveNext();
                reader.VerifyExpectedToken(LuaToken.Comma, LuaToken.RightBracket);
                if (reader.Current.Value == LuaToken.RightBracket)
                    break;
                reader.MoveNext();
            }
            reader.VerifyExpectedTokenAndMoveNext(LuaToken.RightBracket);
            var statementsParser = new StatementBlockParser(Keywords.End);
            var body = statementsParser.ParseBlock(reader, context);

            return new FunctionDeclarationStatement(functionName.Value,argumentNames,body);
        }
        public LuaStatement Parse(INextAwareEnumerator <Token> reader, IParserContext context)
        {
            reader.VerifyExpectedTokenAndMoveNext(Keywords.Function);
            if (reader.Next.Value == LuaToken.Colon)
            {
                reader.MoveNext();
                reader.MoveNext();
            }
            var functionName = reader.GetAndMoveNext();

            reader.VerifyExpectedToken(LuaToken.LeftBracket);

            var argumentNames = new List <string>();

            reader.MoveNext();
            while (reader.Current.Value != LuaToken.RightBracket)
            {
                argumentNames.Add(reader.Current.Value);
                reader.MoveNext();
                reader.VerifyExpectedToken(LuaToken.Comma, LuaToken.RightBracket);
                if (reader.Current.Value == LuaToken.RightBracket)
                {
                    break;
                }
                reader.MoveNext();
            }
            reader.VerifyExpectedTokenAndMoveNext(LuaToken.RightBracket);
            var statementsParser = new StatementBlockParser(Keywords.End);
            var body             = statementsParser.ParseBlock(reader, context);

            return(new FunctionDeclarationStatement(functionName.Value, argumentNames, body));
        }
 public override LuaExpression Parse(INextAwareEnumerator<Token> reader, IParserContext context)
 {
     reader.VerifyExpectedToken(LuaToken.LeftBracket);
     reader.MoveNext();
     var expression = SyntaxParser.ReadExpression(reader, context);
     reader.VerifyExpectedToken(LuaToken.RightBracket);
     reader.MoveNext();
     return new BracketedExpression(expression);
 }
        public LuaExpression Parse(INextAwareEnumerator <Token> reader, IParserContext context)
        {
            reader.VerifyExpectedToken(LuaToken.LeftBracket);
            reader.MoveNext();
            var expression = SyntaxParser.ReadExpression(reader, context);

            reader.VerifyExpectedToken(LuaToken.RightBracket);
            reader.MoveNext();
            return(new BracketedExpression(expression));
        }
 public override LuaExpression Parse(INextAwareEnumerator<Token> reader, IParserContext context)
 {
     reader.VerifyExpectedToken(LuaToken.LeftCurlyBrace);
     reader.MoveNext();
     var expressions = new List<LuaExpression>();
     while (reader.Current.Value != LuaToken.RightCurlyBrace)
     {
         expressions.Add(SyntaxParser.ReadExpression(reader, context));
         reader.VerifyExpectedToken(LuaToken.RightCurlyBrace, LuaToken.Comma);
     }
     reader.VerifyExpectedToken(LuaToken.RightCurlyBrace);
     reader.MoveNext();
     return new TableInitializerExpression(expressions);
 }
 private IList<Variable> ReadDeclarations(INextAwareEnumerator<Token> reader)
 {
     var result = new List<Variable>();
     while (reader.HasNext && reader.Next.Value != null)
     {
         var variable = new Variable(reader.Current.Value);
         result.Add(variable);
         reader.MoveNext();
         reader.VerifyExpectedToken(LuaToken.Comma,LuaToken.EqualsSign);
         if (reader.Current.Value == LuaToken.EqualsSign)
             break;
         reader.MoveNext();
     }
     return result;
 }
        public StatementParser Identify(INextAwareEnumerator<Token> reader)
        {
            if (reader.Current.Value == Keywords.If)
                return new IfStatementParser();
            if (reader.Current.Value == Keywords.While)
                return new WhileStatementParser();
            if (reader.Current.Value == Keywords.Return)
                return new ReturnStatementParser();
            if (reader.Current.Value == Keywords.For)
                return new ForStatementParser();
            if (reader.Current.Value == LuaToken.Semicolon)
                return new EmptyStatementParser();
            if (reader.Current.Value == Keywords.Do)
                return new DoEndBlockStatementParser();

            bool local = false;
            if (reader.Current.Value == Keywords.Local)
            {
                local = true;
                reader.MoveNext();
            }

            if (reader.Current.Value == Keywords.Function)
                return new FunctionDeclarationStatementParser(local);
            return new AssignmentStatementParser(local);
        }
Esempio n. 8
0
        public static Token GetAndMoveNext(this INextAwareEnumerator <Token> enumerator)
        {
            var token = enumerator.Current;

            enumerator.MoveNext();
            return(token);
        }
 public IEnumerable<LuaExpression> Parse(INextAwareEnumerator<Token> reader, IParserContext context)
 {
     var expressions = new List<LuaExpression>();
     do
     {
         expressions.Add(SyntaxParser.ReadExpression(reader, context));
     } while (reader.Current.Value == LuaToken.Comma && reader.MoveNext());
     return expressions;
 }
        private IList <Variable> ReadDeclarations(INextAwareEnumerator <Token> reader)
        {
            var result = new List <Variable>();

            while (reader.HasNext && reader.Next.Value != null)
            {
                var variable = new Variable(reader.Current.Value);
                result.Add(variable);
                reader.MoveNext();
                reader.VerifyExpectedToken(LuaToken.Comma, LuaToken.EqualsSign);
                if (reader.Current.Value == LuaToken.EqualsSign)
                {
                    break;
                }
                reader.MoveNext();
            }
            return(result);
        }
Esempio n. 11
0
        public LuaExpression Parse(INextAwareEnumerator <Token> reader, IParserContext context)
        {
            var constantValue = double.Parse(reader.Current.Value);

            reader.MoveNext();
            return(new ConstantExpression(new LuaValue {
                NumericValue = constantValue
            }));
        }
        public LuaExpression Parse(INextAwareEnumerator <Token> reader, IParserContext context)
        {
            var expression = new ConstantExpression(new LuaValue {
                BooleanValue = bool.Parse(reader.Current.Value)
            });

            reader.MoveNext();
            return(expression);
        }
Esempio n. 13
0
 public static LuaStatement ReadStatement(INextAwareEnumerator<Token> reader, IParserContext context)
 {
     while (string.IsNullOrEmpty(reader.Current.Value) 
         || reader.Current.Value == "\n"
         || reader.Current.Type == TokenType.Comment)
         reader.MoveNext();
     var statementDiscriminator = new StatementParserDiscriminator();
     var statementParser = statementDiscriminator.Identify(reader);
     return statementParser.Parse(reader, context);
 }
Esempio n. 14
0
        public LuaExpression Parse(INextAwareEnumerator <Token> reader, IParserContext context)
        {
            reader.VerifyExpectedToken(LuaToken.LeftCurlyBrace);
            reader.MoveNext();
            var expressions = Enumerable.Empty <LuaExpression>();

            if (reader.Current.Value != LuaToken.RightCurlyBrace)
            {
                var parser = new ExpressionListParser();
                expressions = parser.Parse(reader, context);
                reader.VerifyExpectedToken(LuaToken.RightCurlyBrace);
                reader.MoveNext();
            }
            else
            {
                reader.VerifyExpectedTokenAndMoveNext(LuaToken.RightCurlyBrace);
            }
            return(new TableInitializerExpression(expressions));
        }
        public override LuaExpression Parse(INextAwareEnumerator<Token> reader, IParserContext context)
        {
            var name = reader.Current.Value;
            reader.MoveNext();
            reader.VerifyExpectedToken(LuaToken.LeftBracket);

            var parametersParser = new ExpressionListParser();

            var expression = new FunctionCallExpression(name, parametersParser.Parse(reader, context).ToList());
            reader.VerifyExpectedToken(LuaToken.RightBracket);
            return expression;
        }
 public override LuaStatement Parse(INextAwareEnumerator<Token> reader, IParserContext context)
 {
     var scope = context.AcquireScope();
     reader.VerifyExpectedToken(Keywords.Do);
     reader.MoveNext();
     var statements = new List<LuaStatement>();
     while (reader.Current.Value != Keywords.End)
         statements.Add(SyntaxParser.ReadStatement(reader, context));
     reader.VerifyExpectedTokenAndMoveNext(Keywords.End);
     context.ReleaseScope(scope);
     return new DoEndBlock(new StatementBlock(statements));
 }
Esempio n. 17
0
 public StatementBlock ParseBlock(INextAwareEnumerator<Token> reader, IParserContext context, out Token terminationToken)
 {
     var statements = new List<LuaStatement>();
     while (!_terminatingTokens.Contains(reader.Current.Value))
     {
         statements.Add(SyntaxParser.ReadStatement(reader, context));
         while (string.IsNullOrEmpty(reader.Current.Value) || reader.Current.Value == "\n")
             reader.MoveNext();
     }
     terminationToken = reader.Current;
     reader.VerifyExpectedTokenAndMoveNext(_terminatingTokens);
     return new StatementBlock(statements);
 }
Esempio n. 18
0
        public static LuaStatement ReadStatement(INextAwareEnumerator <Token> reader, IParserContext context)
        {
            while (string.IsNullOrEmpty(reader.Current.Value) ||
                   reader.Current.Value == "\n" ||
                   reader.Current.Type == TokenType.Comment)
            {
                reader.MoveNext();
            }

            var statementParser = StatementParserDiscriminator.Identify(reader);

            return(statementParser.Parse(reader, context));
        }
Esempio n. 19
0
        public IEnumerable <LuaExpression> Parse(INextAwareEnumerator <Token> reader, IParserContext context)
        {
            var expressions = new List <LuaExpression>();

            do
            {
                if (reader.Current.Value == LuaToken.RightBracket)
                {
                    break;
                }
                expressions.Add(SyntaxParser.ReadExpression(reader, context));
            } while (reader.Current.Value == LuaToken.Comma && reader.MoveNext());
            return(expressions);
        }
Esempio n. 20
0
        public LuaExpression Parse(INextAwareEnumerator <Token> reader, IParserContext context)
        {
            var name = reader.Current.Value;

            reader.MoveNext();
            reader.VerifyExpectedTokenAndMoveNext(LuaToken.LeftBracket);

            var parametersParser = new ExpressionListParser();

            var expression = new FunctionCallExpression(name, parametersParser.Parse(reader, context).ToList());

            reader.VerifyExpectedToken(LuaToken.RightBracket);
            return(expression);
        }
 public override LuaExpression Parse(INextAwareEnumerator<Token> reader, IParserContext context)
 {
     var variableName = reader.Current.Value;
     var visibleVariables = context.CurrentScope.GetVisibleVariables();
     Variable variable;
     if (!visibleVariables.ContainsKey(variableName))
     {
         context.AddError("Undefined variable: " + variableName);
         variable = new Variable(variableName);
     }
     else variable = visibleVariables[variableName];
     reader.MoveNext();
     return new VariableExpression(variable);
 }
Esempio n. 22
0
        public LuaExpression Parse(INextAwareEnumerator <Token> reader, IParserContext context)
        {
            var           tableName = reader.Current.Value;
            LuaExpression indexerExpression;

            reader.MoveNext();
            if (reader.Current.Value == LuaToken.Dot)
            {
                reader.MoveNext();
                indexerExpression = new StringConstantExpression(reader.Current.Value);
            }
            else if (reader.Current.Value == LuaToken.LeftSquareBracket)
            {
                reader.MoveNext();
                indexerExpression = SyntaxParser.ReadExpression(reader, context);
                reader.VerifyExpectedTokenAndMoveNext(LuaToken.RightSquareBracket);
            }
            else
            {
                throw new UnexpectedTokenException(reader.Current, LuaToken.Dot, LuaToken.LeftSquareBracket);
            }
            return(new TableIndexExpression(new Variable(tableName), indexerExpression));
        }
Esempio n. 23
0
        public LuaStatement Parse(INextAwareEnumerator <Token> reader, IParserContext context)
        {
            var scope = context.AcquireScope();

            reader.VerifyExpectedToken(Keywords.Do);
            reader.MoveNext();
            var statements = new List <LuaStatement>();

            while (reader.Current.Value != Keywords.End)
            {
                statements.Add(SyntaxParser.ReadStatement(reader, context));
            }
            reader.VerifyExpectedTokenAndMoveNext(Keywords.End);
            context.ReleaseScope(scope);
            return(new DoEndBlock(new StatementBlock(statements)));
        }
Esempio n. 24
0
        public StatementBlock ParseBlock(INextAwareEnumerator <Token> reader, IParserContext context,
                                         out Token terminationToken)
        {
            var statements = new List <LuaStatement>();

            while (!_terminatingTokens.Contains(reader.Current.Value))
            {
                statements.Add(SyntaxParser.ReadStatement(reader, context));
                while (string.IsNullOrEmpty(reader.Current.Value) || reader.Current.Value == "\n")
                {
                    reader.MoveNext();
                }
            }
            terminationToken = reader.Current;
            reader.VerifyExpectedTokenAndMoveNext(_terminatingTokens);
            return(new StatementBlock(statements));
        }
        public LuaExpression Parse(INextAwareEnumerator <Token> reader, IParserContext context)
        {
            var      variableName     = reader.Current.Value;
            var      visibleVariables = context.CurrentScope.GetVisibleVariables();
            Variable variable;

            if (!visibleVariables.ContainsKey(variableName))
            {
                context.AddError("Undefined variable: " + variableName);
                variable = new Variable(variableName);
            }
            else
            {
                variable = visibleVariables[variableName];
            }
            reader.MoveNext();
            return(new VariableExpression(variable));
        }
Esempio n. 26
0
        public static IStatementParser Identify(INextAwareEnumerator <Token> reader)
        {
            if (reader.Current.Value == Keywords.If)
            {
                return(new IfStatementParser());
            }
            if (reader.Current.Value == Keywords.While)
            {
                return(new WhileStatementParser());
            }
            if (reader.Current.Value == Keywords.Return)
            {
                return(new ReturnStatementParser());
            }
            if (reader.Current.Value == Keywords.For)
            {
                return(new ForStatementParser());
            }
            if (reader.Current.Value == LuaToken.Semicolon)
            {
                return(new EmptyStatementParser());
            }
            if (reader.Current.Value == Keywords.Do)
            {
                return(new DoEndBlockStatementParser());
            }

            var local = false;

            if (reader.Current.Value == Keywords.Local)
            {
                local = true;
                reader.MoveNext();
            }

            if (reader.Current.Value == Keywords.Function)
            {
                return(new FunctionDeclarationStatementParser(local));
            }

            // If nothing else, the statement is probably an assignment statement

            return(new AssignmentStatementParser(local));
        }
 public override LuaExpression Parse(INextAwareEnumerator<Token> reader, IParserContext context)
 {
     var expression = new ConstantExpression(new LuaValue { BooleanValue = Boolean.Parse(reader.Current.Value) });
     reader.MoveNext();
     return expression;
 }
 public override LuaExpression Parse(INextAwareEnumerator<Token> reader, IParserContext context)
 {
     var constantValue = Double.Parse(reader.Current.Value);
     reader.MoveNext();
     return new ConstantExpression(new LuaValue {NumericValue = constantValue});
 }
Esempio n. 29
0
 public static void VerifyExpectedTokenAndMoveNext(this INextAwareEnumerator <Token> enumerator,
                                                   params string[] expectedTokens)
 {
     enumerator.VerifyExpectedToken(expectedTokens);
     enumerator.MoveNext();
 }