/// <summary> /// Matches a <c>Index</c> non-terminal. /// </summary> /// <returns><c>true</c> if the <c>Index</c> was matched successfully; otherwise, <c>false</c>.</returns> /// <remarks> /// The non-terminal can start with: <c>Dot</c>, <c>OpenSquareBracket</c>. /// </remarks> protected virtual bool MatchIndex(ref Expression expression) { LuatAstNodeBase index; IToken indexToken; int start = expression.StartOffset; if (this.TokenIs(this.LookAheadToken, LuatTokenId.OpenSquareBracket)) { if (!this.Match(LuatTokenId.OpenSquareBracket)) return false; indexToken = this.Token; Expression indexExpression = null; if (!this.MatchExpression(out indexExpression)) return false; index = indexExpression; if (!this.Match(LuatTokenId.CloseSquareBracket)) return false; expression.EndOffset = this.Token.EndOffset; // Expand to encompass ']' } else if (this.TokenIs(this.LookAheadToken, LuatTokenId.Dot)) { if (!this.Match(LuatTokenId.Dot)) return false; indexToken = this.Token; Identifier indexIdentifier = null; if (!this.MatchIdentifier(out indexIdentifier)) { this.RaiseError( this.Token.EndOffset - 1, "Expected identifier." ); } index = indexIdentifier; } else return false; expression = new IndexExpression( expression, indexToken, index, new TextRange( start, this.Token.EndOffset ) ); return true; }
/// <summary> /// Matches a <c>FunctionName</c> non-terminal. /// </summary> /// <returns><c>true</c> if the <c>FunctionName</c> was matched successfully; otherwise, <c>false</c>.</returns> /// <remarks> /// The non-terminal can start with: <c>Identifier</c>. /// </remarks> protected virtual bool MatchFunctionName(out Expression expression, out bool expectsSelf) { expression = null; Identifier identifier; IToken indexToken; expectsSelf = false; if (!this.MatchIdentifier(out identifier)) return false; expression = new VariableExpression( identifier ); while (this.TokenIs(this.LookAheadToken, LuatTokenId.Dot)) { if (!this.Match(LuatTokenId.Dot)) return false; indexToken = this.Token; if (!this.MatchIdentifier(out identifier)) return false; expression = new IndexExpression( expression, indexToken, identifier ); } if (this.TokenIs(this.LookAheadToken, LuatTokenId.Colon)) { if (!this.Match(LuatTokenId.Colon)) return false; indexToken = this.Token; expectsSelf = true; if (!this.MatchIdentifier(out identifier)) return false; expression = new IndexExpression( expression, indexToken, identifier ); } return true; }