private IfStatement ParseIfElse () { Token start = current; Next (); CheckSyntaxExpected (Token.Type.LeftParenthesis); Token leftParen = current; Next (); Expression condition = ParseExpression (); CheckSyntaxExpected (Token.Type.RightParenthesis); Token rightParen = current; Next (); Statement ifBody = ParseStatement (); Next (); Statement elseBody = null; TextPoint elsePoint = new TextPoint(); if (current.Kind == Token.Type.@else) { elsePoint = new TextPoint(current.StartPosition); elseBody = ParseStatement (); } return new IfStatement (condition, ifBody, elseBody, new TextSpan (start, current), new TextSpan (start, rightParen), new TextPoint (leftParen.StartPosition), new TextPoint (rightParen.StartPosition), elsePoint); }
private LabelStatement ParseLabelStatement () { Token start = current; CheckSyntaxExpected (Token.Type.Identifier); Identifier label = ((IdentifierToken)current).Spelling; Next (); CheckSyntaxExpected (Token.Type.Colon); TextPoint colon = new TextPoint (current.StartPosition); Next (); if (LabelSet.Contains (label.Spelling)) Error (DiagnosticCode.EnclosingLabelShadowed, new TextSpan (start, current)); LabelSet.Add (label.Spelling); Statement labeled = ParseStatement(); LabelSet.Remove (label.Spelling); return new LabelStatement (label, labeled, new TextSpan (start, current), colon); }
private List<Parameter> ParseListParametter () { List<Parameter> result = new List<Parameter>(); if (current.Kind == Token.Type.RightParenthesis) return result; CheckSyntaxExpected (Token.Type.Identifier); Next (); TextPoint comma; while (current.Kind == Token.Type.Comma) { comma = new TextPoint (current.StartPosition); Next (); if (CheckSyntaxExpected (Token.Type.Identifier)) result.Add (new Parameter ((current as IdentifierToken).Spelling, new TextSpan (current, current), comma)); Next (); } return result; }
private ArrayLiteralExpression ParseArrayLiteral () { Token start = current; List<ExpressionListElement> elements = new List<ExpressionListElement> (); TextPoint comma = new TextPoint(); do { Next (); if (current.Kind == Token.Type.RightBracket) break; Expression exp = null; //take care of elision if (current.Kind != Token.Type.Comma) exp = ParseAssignmentExpression (false); ExpressionListElement element = new ExpressionListElement (exp, comma); elements.Add (element); comma = new TextPoint(current.StartPosition); } while (current.Kind == Token.Type.Comma); CheckSyntaxExpected (Token.Type.RightBracket); return new ArrayLiteralExpression (elements, new TextSpan(start, current)); }
private Expression ParseObjectLiteral () { Token start = current; Next (); List<ObjectLiteralElement> elements = new List<ObjectLiteralElement> (); if (current.Kind == Token.Type.RightBrace) { return new ObjectLiteralExpression (elements, new TextSpan (start, current)); } ObjectLiteralElement element; TextPoint comma = new TextPoint (); TextPoint colon = new TextPoint (); Expression name; do { switch (current.Kind) { case Token.Type.Identifier: name = new IdentifierExpression ((current as IdentifierToken).Spelling, new TextSpan (current, current)); break; case Token.Type.NumericLiteral: name = new NumericLiteralExpression (((NumericLiteralToken)current).Spelling, new TextSpan (current, current)); break; case Token.Type.StringLiteral: name = new StringLiteralExpression ((current as StringLiteralToken).Value, (current as StringLiteralToken).Spelling, new TextSpan (current, current)); break; default: Error (DiagnosticCode.SyntaxError, new TextSpan (current, current)); return new Expression (Expression.Operation.SyntaxError, new TextSpan (current, current)); } Next (); CheckSyntaxExpected (Token.Type.Colon); colon = new TextPoint (current.StartPosition); Next (); Expression val = ParseAssignmentExpression (false); element = new ObjectLiteralElement (name, val, colon, comma); elements.Add (element); comma = new TextPoint (current.StartPosition); } while (current.Kind == Token.Type.Comma); CheckSyntaxExpected (Token.Type.RightBrace); elements.Add (element); return new ObjectLiteralExpression (elements, new TextSpan (start, current)); }
private FunctionDefinition ParseFunctionDefinition () { Token start = current; Next (); Identifier id = null; if (CheckSyntaxExpected (Token.Type.Identifier)) id = ((IdentifierToken)current).Spelling; TextPoint NameLocation = new TextPoint (current.StartPosition); Next (); TextPoint leftParenLocation = new TextPoint(); CheckSyntaxExpected (Token.Type.LeftParenthesis); leftParenLocation = new TextPoint (current.StartPosition); Next (); List<Parameter> parametters = ParseListParametter (); TextPoint rightParenLocation = new TextPoint(); CheckSyntaxExpected (Token.Type.RightParenthesis); rightParenLocation = new TextPoint (current.StartPosition); Token headerEnd = current; Next (); BlockStatement body = ParseBlock (); TextSpan location = new TextSpan (start, current); TextSpan HeaderLocation = new TextSpan (start, headerEnd); return new FunctionDefinition (id, parametters, body, location, HeaderLocation, NameLocation, leftParenLocation, rightParenLocation); }