protected override void Error(int lookaheadIndex, string message, params object[] args) { int iPos = GetTextPosition(InputPosition + lookaheadIndex); SourcePos pos = _sourceFile.IndexToLine(iPos); ErrorSink.Write(_Error, pos, message, args); }
void PrintErrorIfTypeMarkerIsKeywordLiteral(object boolOrNull) { if (boolOrNull != NoValue.Value) { ErrorSink.Write(Severity.Error, IndexToPositionObject(_startPosition), "Keyword '{0}' used as a type marker", boolOrNull); } }
void UnhandledException(Exception ex) { int iPos = GetTextPosition(InputPosition); SourcePos pos = _sourceFile.IndexToLine(iPos); ErrorSink.Write(Severity.Critical, pos, "Bug: unhandled exception in parser - " + ex.ExceptionMessageAndType()); }
/// <summary>A method that is called when the indent level changed without /// a corresponding indent trigger.</summary> /// <param name="tokenBeforeNewline">Final non-whitespace token before the newline.</param> /// <param name="tokenAfterNewline">First non-whitespace token after the newline. /// Though it's a <see cref="Maybe{T}"/>, it always has a value, but this /// function can suppress its emission by setting it to NoValue.Value.</param> /// <param name="deltaIndent">Amount of unexpected indentation (positive or /// negative). On return, this parameter holds the amount by which to change /// the <see cref="CurrentIndent"/>; the default implementation leaves this /// value unchanged, which means that subsequent lines will be expected to /// be indented by the same (unexpected) amount.</param> /// <returns>true if <see cref="MakeEndOfLineToken"/> should be called as /// usual, or false to suppress EOL genertion. EOL can only be suppressed /// in case of an unexpected indent (<c>deltaIndent>0</c>), not an unindent.</returns> /// <remarks>The default implementation always returns true. It normally /// writes an error message, but switches to a warning in case /// <c>OuterIndents[OuterIndents.Count-1] == OuterIndents[OuterIndents.Count-2]</c>, /// which this class interprets as a single unindent. /// </remarks> protected virtual bool IndentChangedUnexpectedly(Token tokenBeforeNewline, ref Maybe <Token> tokenAfterNewline, ref int deltaIndent) { var pos = IndexToMsgContext(tokenAfterNewline.Or(default(Token))); if (deltaIndent > 0) { if (_errorBias >= 0) { ErrorSink.Error(pos, "Unexpected indent"); } _errorBias++; } else { if (_errorBias <= 0) { var sev = Severity.Error; if (_outerIndents.Count >= 2 && _outerIndents.Last == _outerIndents[_outerIndents.Count - 2]) { sev = Severity.Warning; } ErrorSink.Write(sev, pos, "Unindent does not match any outer indentation level"); } _errorBias--; } return(true); }
void UnhandledException(Exception ex) { int iPos = GetTextPosition(InputPosition); SourceRange pos = new SourceRange(_sourceFile, iPos); ErrorSink.Write(Severity.Critical, pos, "Bug: unhandled exception in parser - " + ex.ExceptionMessageAndType()); }
LNode ParseHostReturnType(Token paren) { var list = ParseHostCode(paren, ParsingMode.FormalArguments); if (list.Count != 1) { Error(-1, "LLLPG: Expected a single variable declaration (or data type) after '{0}'", ToString(paren.TypeInt)); } LNode result; if (list.Count > 0) { result = list[0]; } else { result = LNode.Missing; } if (result.Calls(S.Var, 2)) { if (!result[1].IsIdNamed("result")) { ErrorSink.Write(Severity.Error, result[1], "LLLPG requires that the result of a rule be called 'result'"); } return(result[0]); } else { return(result); } }
public void CheckForSpaceAtEndOfAttribute() { if (LT0.StartIndex == LT(-1).EndIndex) { var location = new SourceRange(SourceFile, LT0.StartIndex); ErrorSink.Write(Severity.Error, location, "Expected space after attribute"); } }
public void CheckForSpace(bool expectSpace, string errorMsg) { if ((LT0.StartIndex == LT(-1).EndIndex) == expectSpace) { var location = new SourceRange(SourceFile, LT0.StartIndex); ErrorSink.Write(Severity.Error, location, errorMsg); } }
protected LNode MissingExpr(Token tok, string error = null, bool afterToken = false) { int startIndex = afterToken ? tok.EndIndex : tok.StartIndex; LNode missing = F.Id(S.Missing, startIndex, tok.EndIndex); if (error != null) { ErrorSink.Write(Severity.Error, missing.Range, error); } return(missing); }
protected void WriteError(int index, string msg, params object[] args) { if (ErrorSink == null) { throw new FormatException(MessageSink.FormatMessage(Severity.Error, SourceFile.IndexToLine(index), msg, args)); } else { ErrorSink.Write(Severity.Error, SourceFile.IndexToLine(index), msg, args); } }
protected Precedence PrefixPrecedenceOf(Token t) { var prec = _prec.Find(OperatorShape.Prefix, t.Value); if (prec == LesPrecedence.Other) { ErrorSink.Write(Severity.Error, F.Id(t), "Operator `{0}` cannot be used as a prefix operator", t.Value); } return(prec); }
protected virtual void CheckForIndentStyleMismatch(UString indent1, UString indent2, Token next) { int common = System.Math.Min(indent1.Length, indent2.Length); indent1 = indent1.Substring(0, common); indent2 = indent2.Substring(0, common); if (!indent1.Equals(indent2)) { ErrorSink.Write(Severity.Warning, IndexToMsgContext(next), "Indentation style changed on this line from {0} to {1}", Les2Printer.PrintLiteral(indent1.ToString()), Les2Printer.PrintLiteral(indent2.ToString())); } }
protected override void Error(int index, string message) { // the fast "blitting" code path may not be able to handle errors _parseNeeded = true; var pos = SourceFile.IndexToLine(index); if (ErrorSink != null) { ErrorSink.Write(Severity.Error, pos, message); } else { throw new FormatException(pos + ": " + message); } }
public new virtual void Error(int lookaheadIndex, string format, params object[] args) { int index = InputPosition + lookaheadIndex; SourcePos pos; if (SourceFile == null) { pos = new SourcePos(FileName, LineNumber, index - LineStartAt + 1); } else { pos = SourceFile.IndexToLine(index); } if (ErrorSink != null) { if (args != null) { ErrorSink.Write(Severity.Error, pos, format, args); } else { ErrorSink.Write(Severity.Error, pos, format); } } else { string msg; if (args != null) { msg = Localize.From(format, args); } else { msg = Localize.From(format); } throw new FormatException(pos + ": " + msg); } }
object ParseLiteral2(UString typeMarker, UString parsedText, bool isNumericLiteral) { if (SkipValueParsing) { return(null); } if (PreferCustomLiterals) { return(new CustomLiteral(parsedText.ToString(), (Symbol)typeMarker.ToString())); } else { string syntaxError; var result = ParseLiteral(typeMarker, parsedText, isNumericLiteral, out syntaxError); if (syntaxError != null) { var pos = new SourceRange(SourceFile, _startPosition, InputPosition - _startPosition); ErrorSink.Write(Severity.Error, pos, syntaxError); } return(result); } }
private bool?Evaluate(LNode expr) { if (expr.IsId) { return(DefinedSymbols.Contains(expr.Name)); } else if (expr.IsLiteral && expr.Value is bool) { return((bool)expr.Value); } else if (expr.Calls(S.And, 2)) { return(Evaluate(expr.Args[0]) & Evaluate(expr.Args[1])); } else if (expr.Calls(S.Or, 2)) { return(Evaluate(expr.Args[0]) | Evaluate(expr.Args[1])); } else if (expr.Calls(S.Not, 1)) { return(!Evaluate(expr.Args[0])); } else if (expr.Calls(S.Eq, 2)) { return(Evaluate(expr.Args[0]) == Evaluate(expr.Args[1])); } else if (expr.Calls(S.Neq, 2)) { return(Evaluate(expr.Args[0]) != Evaluate(expr.Args[1])); } else { ErrorSink.Write(Severity.Error, expr.Range, "Only simple boolean expressions with &&, ||, !, ==, !=, are supported in #if and #elif"); return(null); } }
protected LNode TopExpr() { TT la0, la1; Token at = default(Token); VList <LNode> attrs = default(VList <LNode>); LNode e = default(LNode); Token t = default(Token); // Line 91: greedy((TT.At)? TT.LBrack ExprList TT.RBrack)? do { la0 = (TT)LA0; if (la0 == TT.At) { la1 = (TT)LA(1); if (la1 == TT.LBrack) { goto match1; } } else if (la0 == TT.LBrack) { switch ((TT)LA(1)) { case TT.Assignment: case TT.At: case TT.BQString: case TT.Colon: case TT.Comma: case TT.Dot: case TT.Id: case TT.LBrace: case TT.LBrack: case TT.Literal: case TT.LParen: case TT.NormalOp: case TT.Not: case TT.PrefixOp: case TT.PreOrSufOp: case TT.RBrack: case TT.Semicolon: case TT.SpaceLParen: goto match1; } } break; match1: { // Line 91: (TT.At)? la0 = (TT)LA0; if (la0 == TT.At) { at = MatchAny(); } // line 92 if (at.Type() == default(TT)) { ErrorSink.Write(Severity.Warning, LaIndexToSourcePos(0), "Attribute: expected '@['"); } t = Match((int)TT.LBrack); attrs = ExprList(); Match((int)TT.RBrack); } } while (false); // Line 95: (Expr / TT.Id Expr (Particle)*) do { switch ((TT)LA0) { case TT.Assignment: case TT.BQString: case TT.Dot: case TT.NormalOp: case TT.Not: case TT.PrefixOp: case TT.PreOrSufOp: e = Expr(StartStmt); break; case TT.Id: { switch ((TT)LA(1)) { case TT.Assignment: case TT.BQString: case TT.Dot: case TT.NormalOp: e = Expr(StartStmt); break; case TT.Colon: { if (LA(1 + 1) != (int)TT.Indent) { e = Expr(StartStmt); } else { goto match2; } } break; case EOF: case TT.Comma: case TT.Dedent: case TT.LBrack: case TT.LParen: case TT.Not: case TT.PreOrSufOp: case TT.RBrace: case TT.RBrack: case TT.RParen: case TT.Semicolon: e = Expr(StartStmt); break; default: goto match2; } } break; default: e = Expr(StartStmt); break; } break; match2: { var id = MatchAny(); // line 98 var args = VList <LNode> .Empty; args.Add(Expr(P.SuperExpr)); // Line 100: (Particle)* for (;;) { switch ((TT)LA0) { case TT.At: case TT.Colon: case TT.Id: case TT.LBrace: case TT.LBrack: case TT.Literal: case TT.LParen: case TT.SpaceLParen: { // line 101 if (((TT)LA0 == TT.LParen)) { var loc = args[args.Count - 2, args.Last].Range.End; Error(0, "Expected a space before '(' (possibly missing ';' or ',' at {0})", loc); } args.Add(Particle()); } break; default: goto stop; } } stop :; // line 108 e = MarkSpecial(F.Call(id, args, id.StartIndex, args.Last.Range.EndIndex)); } } while (false); if ((t.TypeInt != 0)) { e = e.WithRange(t.StartIndex, e.Range.EndIndex); } return(e.PlusAttrs(attrs)); }
/// <inheritdoc cref="Error(int,string)"/> protected virtual void Error(int lookaheadIndex, string format, params object[] args) { ErrorSink.Write(Severity.Error, LaIndexToSourcePos(lookaheadIndex), format, args); }
private void Error(Token pptoken, string message) { ErrorSink.Write(Severity.Error, pptoken.ToSourceRange(SourceFile), message); }
protected void Error(LNode node, string message, params object[] args) { ErrorSink.Write(_Error, node, message, args); }
/// <summary>Records an error or throws an exception.</summary> /// <param name="lookaheadIndex">Location of the error relative to the /// current <c>InputPosition</c>. When called by BaseParser, lookaheadIndex /// is always equal to 0.</param> /// <remarks> /// The default implementation throws a <see cref="FormatException"/>. /// When overriding this method, you can convert the lookaheadIndex /// to a <see cref="SourcePos"/> using the expression /// <c>SourceFile.IndexToLine(LT(lookaheadIndex).StartIndex)</c>. This only /// works if an <c>ISourceFile</c> object was provided to the constructor of /// this class, and <c>Token</c> implements <see cref="ISimpleToken"/>. /// </remarks> protected virtual void Error(int lookaheadIndex, string message) { ErrorSink.Write(Severity.Error, LaIndexToSourcePos(lookaheadIndex), message); }
protected override void Error(int li, string message) { int iPos = GetTextPosition(InputPosition + li); ErrorSink.Write(Severity.Error, (_sourceFile ?? EmptySourceFile.Unknown).IndexToLine(iPos), message); }
protected void Error(Token token, string message, params object[] args) { ErrorSink.Write(_Error, _sourceFile.IndexToLine(token.StartIndex), message, args); }
protected LNode TopExpr() { TT la0, la1; Token at = default(Token); LNodeList attrs = default(LNodeList); LNode e = default(LNode); Token t = default(Token); // line 93 var attrStart = int.MaxValue; // Line 95: greedy(TT.At TT.LBrack ExprList TT.RBrack)* for (;;) { la0 = (TT)LA0; if (la0 == TT.At) { la1 = (TT)LA(1); if (la1 == TT.LBrack) { at = MatchAny(); // line 96 if (at.Type() == default(TT)) { ErrorSink.Write(Severity.Warning, LaIndexToMsgContext(0), "Attribute: expected '@['"); } else { attrStart = System.Math.Min(attrStart, at.StartIndex); } t = MatchAny(); attrs = ExprList(attrs); Match((int)TT.RBrack); } else { break; } } else { break; } } // Line 100: (Expr / TT.Id Expr (Particle)*) switch ((TT)LA0) { case TT.Assignment: case TT.BQOperator: case TT.Dot: case TT.NormalOp: case TT.Not: case TT.PrefixOp: case TT.PreOrSufOp: e = Expr(StartStmt); break; case TT.Id: { switch ((TT)LA(1)) { case EOF: case TT.Assignment: case TT.BQOperator: case TT.Comma: case TT.Dot: case TT.LBrack: case TT.LParen: case TT.NormalOp: case TT.Not: case TT.PreOrSufOp: case TT.RBrace: case TT.RBrack: case TT.RParen: case TT.Semicolon: e = Expr(StartStmt); break; default: { var id = MatchAny(); // line 103 var args = LNodeList.Empty; args.Add(Expr(P.SuperExpr)); // Line 105: (Particle)* for (;;) { switch ((TT)LA0) { case TT.At: case TT.Id: case TT.LBrace: case TT.LBrack: case TT.Literal: case TT.LParen: case TT.SpaceLParen: { // line 106 if ((((TT)LA0) == TT.LParen)) { var loc = args[args.Count - 2, args.Last].Range.End; Error(0, "Expected a space before '(' (possibly missing ';' or ',' at {0})", loc); } args.Add(Particle()); } break; default: goto stop; } } stop :; // line 113 e = MarkSpecial(F.Call(id, args, id.StartIndex, args.Last.Range.EndIndex)); } break; } } break; default: e = Expr(StartStmt); break; } // line 115 if ((attrStart < e.Range.StartIndex)) { e = e.WithRange(attrStart, e.Range.EndIndex); } // line 116 return(e.PlusAttrsBefore(attrs)); }
protected override void Error(int index, string message) { _parseNeeded = true; // don't use the "fast" code path ErrorSink.Write(Severity.Error, SourceFile.IndexToLine(index), message); }
public override Token?NextToken() { do { Token?t_ = _source.NextToken(); redo: if (t_ == null) { break; } var t = t_.Value; if (t.IsWhitespace) { if (t.Kind == TokenKind.Comment) { AddComment(t); } continue; } else if (t.Kind == TokenKind.Other) { switch (t.Type()) { case TokenType.PPdefine: case TokenType.PPundef: ReadRest(); bool undef = t.Type() == TokenType.PPundef; if (_rest.Count == 1 && _rest[0].Type() == TokenType.Id) { if (undef) { DefinedSymbols.Remove((Symbol)_rest[0].Value); } else { DefinedSymbols.Add((Symbol)_rest[0].Value); } } else { ErrorSink.Write(Severity.Error, t.ToSourceRange(SourceFile), "'{0}' should be followed by a single, simple identifier", undef ? "#undef" : "#define"); } continue; case TokenType.PPif: var tree = ReadRestAsTokenTree(); LNode expr = ParseExpr(tree); var cond = Evaluate(expr) ?? false; _ifRegions.Push(Pair.Create(t, cond)); t_ = SaveDirectiveAndAutoSkip(t, cond); goto redo; case TokenType.PPelse: case TokenType.PPelif: var tree_ = ReadRestAsTokenTree(); if (_ifRegions.Count == 0) { ErrorSink.Write(Severity.Error, t.ToSourceRange(SourceFile), "Missing #if clause before '{0}'", t); _ifRegions.Push(Pair.Create(t, false)); } bool isElif = t.Type() == TokenType.PPelif, hasExpr = tree_.HasIndex(0); if (hasExpr != isElif) { Error(t, isElif ? "Missing condition on #elif" : "Unexpected tokens after #else"); } bool cond_ = true; if (hasExpr) { LNode expr_ = ParseExpr(tree_); cond_ = Evaluate(expr_) ?? false; } if (_ifRegions.Peek().B) { cond_ = false; } t_ = SaveDirectiveAndAutoSkip(t, cond_); if (cond_) { _ifRegions.Push(Pair.Create(_ifRegions.Pop().A, cond_)); } goto redo; case TokenType.PPendif: var tree__ = ReadRestAsTokenTree(); if (_ifRegions.Count == 0) { Error(t, "Missing #if before #endif"); } else { _ifRegions.Pop(); if (tree__.Count > 0) { Error(t, "Unexpected tokens after #endif"); } } _commentList.Add(t); continue; case TokenType.PPerror: _commentList.Add(t); Error(t, t.Value.ToString()); continue; case TokenType.PPwarning: _commentList.Add(t); ErrorSink.Write(Severity.Warning, t.ToSourceRange(SourceFile), t.Value.ToString()); continue; case TokenType.PPregion: _commentList.Add(t); _regions.Push(t); continue; case TokenType.PPendregion: _commentList.Add(t); if (_regions.Count == 0) { ErrorSink.Write(Severity.Warning, t.ToSourceRange(SourceFile), "#endregion without matching #region"); } else { _regions.Pop(); } continue; case TokenType.PPline: _commentList.Add(new Token(t.TypeInt, t.StartIndex, _source.InputPosition)); var rest = ReadRestAsTokenTree(); // TODO ErrorSink.Write(Severity.Note, t.ToSourceRange(SourceFile), "Support for #line is not implemented"); continue; case TokenType.PPpragma: _commentList.Add(new Token(t.TypeInt, t.StartIndex, _source.InputPosition)); var rest_ = ReadRestAsTokenTree(); // TODO ErrorSink.Write(Severity.Note, t.ToSourceRange(SourceFile), "Support for #pragma is not implemented"); continue; } } return(t_); } while (true); // end of stream if (_ifRegions.Count > 0) { ErrorSink.Write(Severity.Error, _ifRegions.Peek().A.ToSourceRange(SourceFile), "#if without matching #endif"); } if (_regions.Count > 0) { ErrorSink.Write(Severity.Warning, _regions.Peek().ToSourceRange(SourceFile), "#region without matching #endregion"); } return(null); }
void MissingEndMarker(LNode previousExpr, TokenType endMarker) { var location = new SourceRange(SourceFile, LT(-1).EndIndex + 1); ErrorSink.Write(Severity.Error, location, "Expected '{0}'", endMarker == TT.Comma ? ',' : ';'); }
private void PrintShortInteger(object value, NodeStyle style, string type) { ErrorSink.Write(Severity.Warning, null, "LesNodePrinter: Encountered literal of type '{0}'. It will be printed as 'Int32'.", type); PrintIntegerToString(value, style, ""); }
/// <summary>This method is called to format and handle errors that occur /// during lexing. The default implementation sends errors to <see cref="ErrorSink"/>, /// which, by default, throws a <see cref="FormatException"/>.</summary> /// <param name="lookaheadIndex">Index where the error occurred, relative to /// the current InputPosition (i.e. InputPosition + lookaheadIndex is the /// position of the error).</param> /// <param name="format">An error description with argument placeholders.</param> /// <param name="args">Arguments to insert into the error message.</param> protected virtual void Error(int lookaheadIndex, string format, params object[] args) { var pos = IndexToPositionObject(InputPosition + lookaheadIndex); ErrorSink.Write(Severity.Error, pos, format, args); }
LNode Particle() { TT la0; Token c = default(Token); Token o = default(Token); LNode result = default(LNode); TokenTree tree = default(TokenTree); // Line 186: ( TT.Id | TT.Literal | TT.At (TT.LBrack TokenTree TT.RBrack | TT.LBrace TokenTree TT.RBrace) | TT.Colon TT.Indent StmtList TT.Dedent greedy(TT.Colon)? | TT.LBrace StmtList TT.RBrace | TT.LBrack ExprList TT.RBrack | (TT.LParen|TT.SpaceLParen) ExprList TT.RParen ) switch ((TT)LA0) { case TT.Id: { var id = MatchAny(); // line 187 result = F.Id(id).SetStyle(id.Style); } break; case TT.Literal: { var lit = MatchAny(); // line 189 result = F.Literal(lit).SetStyle(lit.Style); } break; case TT.At: { o = MatchAny(); // Line 192: (TT.LBrack TokenTree TT.RBrack | TT.LBrace TokenTree TT.RBrace) la0 = (TT)LA0; if (la0 == TT.LBrack) { Skip(); tree = TokenTree(); c = Match((int)TT.RBrack); } else { Match((int)TT.LBrace); tree = TokenTree(); c = Match((int)TT.RBrace); } // line 194 result = F.Literal(tree, o.StartIndex, c.EndIndex); } break; case TT.Colon: { o = MatchAny(); Match((int)TT.Indent); var list = StmtList(); c = Match((int)TT.Dedent); // Line 196: greedy(TT.Colon)? la0 = (TT)LA0; if (la0 == TT.Colon) { Skip(); } // line 197 result = F.Braces(list, o.StartIndex, c.EndIndex); } break; case TT.LBrace: { o = MatchAny(); var list = StmtList(); c = Match((int)TT.RBrace); // line 200 result = F.Braces(list, o.StartIndex, c.EndIndex).SetStyle(NodeStyle.Statement); } break; case TT.LBrack: { o = MatchAny(); var list = ExprList(); c = Match((int)TT.RBrack); // line 202 result = F.Call(S.Array, list, o.StartIndex, c.EndIndex).SetStyle(NodeStyle.Expression); } break; case TT.LParen: case TT.SpaceLParen: { // line 204 var endMarker = default(TT); o = MatchAny(); // line 205 var hasAttrList = (TT)LA0 == TT.LBrack || (TT)LA0 == TT.At; var list = ExprList(ref endMarker); c = Match((int)TT.RParen); // line 208 if ((endMarker == TT.Semicolon || list.Count != 1)) { result = F.Call(S.Tuple, list, o.StartIndex, c.EndIndex); if ((endMarker == TT.Comma)) { var msg = "Tuples require ';' as a separator."; if ((o.Type() == TT.SpaceLParen)) { msg += " If a function call was intended, remove the space(s) before '('."; } ErrorSink.Write(Severity.Error, list[0].Range.End, msg); } } else { result = hasAttrList ? list[0] : F.InParens(list[0], o.StartIndex, c.EndIndex); } } break; default: { // line 222 Error(0, "Expected a particle (id, literal, {braces} or (parens))."); result = MissingExpr(); } break; } return(result); }