public void ParseBlock( Block block, Tokens tokens ) { bool enclosed = false; if ( tokens.PeekToken () == BeginBlock ) { enclosed = true; tokens.RemoveNextToken ( BeginBlock ); } while ( !tokens.AtEnd () ) { if ( enclosed && tokens.PeekToken () == EndBlock ) { tokens.RemoveNextToken ( EndBlock ); break; } bool parsed = false; foreach ( IStatementParser parser in _parsers ) { IStatement nextStatement; if ( parser.TryParse( tokens, block.Scope, out nextStatement ) ) { block.Add ( nextStatement ); parsed = true; break; } } if (!parsed) throw new Exception("Unable to parse token " + tokens.PeekToken() ); } }
public Expression ParseExpression( IScope scope, Tokens tokens ) { Expression leftExpression = null; // Get the left value leftExpression = ParseValue ( scope, tokens ); if ( leftExpression == null ) { throw new Exception ( "Expecting a value" ); } // Check if there is an operator which will continue the expression if ( !tokens.AtEnd () ) { ArithOp op = ParseOperator ( tokens ); if ( op != ArithOp.none ) { Expression rightExpression = ParseExpression ( scope, tokens ); if ( rightExpression != null ) { ArithExpr arithExpression = new ArithExpr { Scope = scope, Left = leftExpression, Op = op, Right = rightExpression }; return arithExpression; } } } return leftExpression; }