public static Exception ArrayInitializeByListError(int openBracketTokenPos, TokFlow flow) { var res = ErrorsHelper.GetExpressionListError(openBracketTokenPos, flow, TokType.ArrOBr, TokType.ArrCBr); var list = res.Parsed; var argStubs = ErrorsHelper.CreateArgumentsStub(list); switch (res.Type) { case ExprListErrorType.FirstElementMissed: return(new FunParseException(401, $"[ ??? , ..] <- First element missed {Nl}Remove ',' or place element before it", res.Interval)); case ExprListErrorType.ElementMissed: return(new FunParseException(404, $"[{argStubs},???, ..] <- element missed {Nl}Remove ',' or place element before it", res.Interval)); case ExprListErrorType.TotalyWrongDefinition: return(new FunParseException(407, "Wrong array definition ", res.Interval)); case ExprListErrorType.SingleOpenBracket: return(new FunParseException(410, $"[ <- unexpected array symbol{Nl} Did you mean array initialization [,], slice [::] or indexing [i]?", res.Interval)); case ExprListErrorType.SepIsMissing: return(new FunParseException(413, $"[{argStubs}, ??? , ... <- Seems like ',' is missing{Nl} Example: [{argStubs}, myArgument, ...]", res.Interval)); case ExprListErrorType.ArgumentIsInvalid: return(new FunParseException(416, $"[{argStubs}, ??? , ... <- Seems like array argument is invalid{Nl} Example: [{argStubs}, myArgument, ...]", res.Interval)); case ExprListErrorType.CloseBracketIsMissing: return(new FunParseException(419, $"[{argStubs} ??? <- Array close bracket ']' is missing{Nl} Example: [{argStubs}]", res.Interval)); case ExprListErrorType.LastArgumentIsInvalid: return(new FunParseException(422, $"[{ErrorsHelper.CreateArgumentsStub(list.Take(list.Length-1))} ??? ] <- Seems like array argument is invalid{Nl} Example: [{argStubs}, myArgument, ...]", res.Interval)); default: throw new ArgumentOutOfRangeException(); } }
public static Exception BracketExpressionListError(int openBracketTokenPos, TokFlow flow) { var res = ErrorsHelper.GetExpressionListError(openBracketTokenPos, flow, TokType.Obr, TokType.Cbr); var list = res.Parsed; var argStubs = ErrorsHelper.CreateArgumentsStub(list); switch (res.Type) { case ExprListErrorType.FirstElementMissed: return(new FunParseException(449, $"( ??? , ..) <- First element missed {Nl}Remove ',' or place element before it", res.Interval)); case ExprListErrorType.ElementMissed: return(new FunParseException(452, $"({argStubs},???, ..) <- element missed {Nl}Remove ',' or place element before it", res.Interval)); case ExprListErrorType.TotalyWrongDefinition: return(new FunParseException(455, "Wrong expression", res.Interval)); case ExprListErrorType.SingleOpenBracket: return(new FunParseException(458, $"( <- unexpected bracket{Nl} ?", res.Interval)); case ExprListErrorType.SepIsMissing: return(new FunParseException(461, $"({argStubs}, ??? , ... <- Seems like ',' is missing{Nl} Example: ({argStubs}, myArgument, ...)", res.Interval)); case ExprListErrorType.ArgumentIsInvalid: return(new FunParseException(464, $"({argStubs}, ??? , ... <- Seems like invalid expressions{Nl} Example: ({argStubs}, myArgument, ...)", res.Interval)); case ExprListErrorType.CloseBracketIsMissing: return(new FunParseException(467, $"({argStubs}, ??? <- Close bracket ')' is missing{Nl} Example:({argStubs})", res.Interval)); case ExprListErrorType.LastArgumentIsInvalid: return(new FunParseException(470, $"({ErrorsHelper.CreateArgumentsStub(list.Take(list.Length-1))} ??? ) <- Seems like invalid expression{Nl} Example: ({argStubs}, myArgument, ...)", res.Interval)); default: throw new ArgumentOutOfRangeException(); } }
private SyntaxTree ParseTree(TokFlow flow) { while (true) { flow.SkipNewLines(); if (flow.IsDoneOrEof()) { break; } _attributes = flow.ReadAttributes(); _startOfTheLine = flow.IsStartOfTheLine(); _exprStartPosition = flow.Current.Start; var e = SyntaxNodeReader.ReadNodeOrNull(flow) ?? throw ErrorFactory.UnknownValueAtStartOfExpression(_exprStartPosition, flow.Current); if (e is TypedVarDefSyntaxNode typed) { if (flow.IsCurrent(TokType.Def)) { ReadEquation(typed, typed.Id); } else { ReadInputVariableSpecification(typed); } } else if (flow.IsCurrent(TokType.Def) || flow.IsCurrent(TokType.Colon)) { if (e is NamedIdSyntaxNode variable) { ReadEquation(variable, variable.Id); } //Fun call can be used as fun definition else if (e is FunCallSyntaxNode fun && !fun.IsOperator) { ReadUserFunction(fun); } else { throw ErrorFactory.ExpressionBeforeTheDefinition(_exprStartPosition, e, flow.Current); } }
private static ExprListError SpecifyArrayInitError( IList <ISyntaxNode> arguments, TokFlow flow, TokType openBrack, TokType closeBrack) { var firstToken = flow.Current; int lastArgPosition = arguments.LastOrDefault()?.Interval.Finish ?? flow.Position; flow.SkipNewLines(); var hasAnyBeforeStop = flow.MoveUntilOneOfThe( TokType.Sep, openBrack, closeBrack, TokType.NewLine, TokType.Eof) .Any(); if (firstToken.Is(TokType.Sep)) { //[x,y, {someshit} , ... return(new ExprListError( ExprListErrorType.ArgumentIsInvalid, arguments, new Interval(firstToken.Start, flow.Position))); } var errorStart = lastArgPosition; if (flow.Position == errorStart) { errorStart = arguments.Last().Interval.Start; } //[x, {y someshit} , ... if (!hasAnyBeforeStop) { //[x,y {no ']' here} return(new ExprListError( ExprListErrorType.CloseBracketIsMissing, arguments, new Interval(errorStart, flow.Position))); } //LastArgument is a part of error return(new ExprListError( ExprListErrorType.LastArgumentIsInvalid, arguments, new Interval(arguments.Last().Interval.Start, flow.Position))); }
public static Exception NowNewLineBeforeAttribute(TokFlow flow) => new FunParseException(303, $"Attribute has to start from new line.", flow.Current.Interval);
public static Exception NowNewLineAfterAttribute(int start, TokFlow flow) => new FunParseException(300, $"Attribute needs new line after it.", start, flow.Current.Interval.Finish);
public static Exception AttributeCbrMissed(int start, TokFlow flow) => new FunParseException(297, $"')' is missed but was '{flow.Current}'", start, flow.Current.Interval.Finish);
public static ExprListError GetExpressionListError( int openBracketTokenPos, TokFlow flow, TokType openBrack, TokType closeBrack) { flow.Move(openBracketTokenPos); var obrStart = flow.Current.Start; flow.MoveIfOrThrow(openBrack); var list = new List <ISyntaxNode>(); int currentToken = flow.CurrentTokenPosition; do { if (flow.IsCurrent(TokType.Sep)) { //[,] <-first element missed if (!list.Any()) { return(new ExprListError( ExprListErrorType.FirstElementMissed, list, new Interval(flow.Current.Start, flow.Current.Finish))); } //[x, ,y] <- element missed return(new ExprListError( ExprListErrorType.ElementMissed, list, new Interval(list.Last().Interval.Finish, flow.Current.Finish))); } var exp = SyntaxNodeReader.ReadNodeOrNull(flow); if (exp != null) { list.Add(exp); } if (exp == null && list.Any()) { flow.Move(currentToken); //[x,y, {no or bad expression here} ... return(SpecifyArrayInitError(list, flow, openBrack, closeBrack)); } currentToken = flow.CurrentTokenPosition; } while (flow.MoveIf(TokType.Sep)); if (flow.Current.Is(closeBrack)) { //everything seems fine... return(new ExprListError( ExprListErrorType.TotalyWrongDefinition, list, new Interval(obrStart, flow.Current.Finish))); } if (!list.Any()) { return(new ExprListError(ExprListErrorType.SingleOpenBracket, list, new Interval(obrStart, obrStart + 1))); } var position = flow.CurrentTokenPosition; var nextExpression = SyntaxNodeReader.ReadNodeOrNull(flow); flow.Move(position); if (nextExpression != null)//[x y] <- separator is missed { return(new ExprListError(ExprListErrorType.SepIsMissing, list, new Interval(list.Last().Interval.Finish, nextExpression.Interval.Start))); } //[x {some crappy crap here}] return(SpecifyArrayInitError(list, flow, openBrack, closeBrack)); }
public static (object, FunnyType) ParseValue(this TokFlow flow) { var syntaxNode = SyntaxNodeReader.ReadNodeOrNull(flow); return(ParseSyntaxNode(syntaxNode)); }
private Parser(TokFlow flow) { _flow = flow; }
public static SyntaxTree Parse(TokFlow flow) => new Parser(flow).ParseTree(flow);