1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace NTERA.Engine.Compiler
- {
- public class Parser
- {
- protected Lexer Lexer { get; }
- protected FunctionDefinition SelfDefinition { get; }
- protected ICollection<FunctionDefinition> FunctionDefinitions { get; }
- protected ICollection<FunctionDefinition> ProcedureDefinitions { get; }
- protected ICollection<FunctionVariable> ConstantDefinitions { get; }
- protected List<ParserError> Errors { get; } = new List<ParserError>();
- protected List<ParserError> Warnings { get; } = new List<ParserError>();
- protected VariableDictionary GlobalVariables { get; }
- protected VariableDictionary LocalVariables { get; }
- protected ICollection<Keyword> ExplicitKeywords { get; }
- protected CSVDefinition CsvDefinition { get; }
- protected IEnumerator<Token> Enumerator { get; }
- protected bool hasPeeked = false;
- protected Token peekedToken = Token.Unknown;
- protected Token GetNextToken(bool peek = false)
- {
- if (peek && hasPeeked)
- return peekedToken;
- if (!hasPeeked)
- Enumerator.MoveNext();
- peekedToken = Enumerator.Current;
- hasPeeked = peek;
- return Enumerator.Current;
- }
- protected Marker CurrentPosition => new Marker(Lexer.TokenMarker.Pointer + SelfDefinition.Position.Pointer,
- Lexer.TokenMarker.Line + SelfDefinition.Position.Line - 1,
- Lexer.TokenMarker.Column);
- public Parser(string input, FunctionDefinition selfDefinition, ICollection<FunctionDefinition> functionDefinitions, ICollection<FunctionDefinition> procedureDefinitions, VariableDictionary globalVariables, VariableDictionary localVariables, ICollection<Keyword> explicitKeywords, CSVDefinition csvDefinition, ICollection<FunctionVariable> constantDefnitions)
- {
- Lexer = new Lexer(input);
- Enumerator = Lexer.GetEnumerator();
- SelfDefinition = selfDefinition;
- FunctionDefinitions = functionDefinitions;
- ProcedureDefinitions = procedureDefinitions;
- ConstantDefinitions = constantDefnitions;
- GlobalVariables = globalVariables;
- LocalVariables = localVariables;
- ExplicitKeywords = explicitKeywords;
- CsvDefinition = csvDefinition;
- }
- public IEnumerable<ExecutionNode> Parse(out List<ParserError> errors, out List<ParserError> warnings)
- {
- List<ExecutionNode> nodes = new List<ExecutionNode>();
- using (Enumerator)
- {
- do
- {
- var node = ParseLine(out var error);
- if (error != null)
- {
- Errors.Add(error);
- nodes.Add(new ExecutionNode
- {
- Type = "error",
- Metadata =
- {
- ["message"] = error.ErrorMessage,
- ["symbol"] = error.SymbolMarker.ToString()
- },
- Symbol = error.SymbolMarker
- });
- //resynchronize to a new line
- while (Enumerator.MoveNext()
- && Enumerator.Current != Token.NewLine
- && Enumerator.Current != Token.EOF)
- {
- }
- }
- else if (node != null)
- {
- nodes.Add(node);
- }
- hasPeeked = false;
- } while (Enumerator.MoveNext());
- }
- errors = Errors;
- warnings = Warnings;
- return nodes;
- }
- protected ExecutionNode ParseLine(out ParserError error)
- {
- error = null;
- switch (Enumerator.Current)
- {
- case Token.Identifer:
- if (GlobalVariables.ContainsKey(Lexer.Identifier)
- || LocalVariables.ContainsKey(Lexer.Identifier)
- || ConstantDefinitions.Any(x => x.Name.Equals(Lexer.Identifier, StringComparison.OrdinalIgnoreCase)))
- {
- string variableName = Lexer.Identifier;
- ValueType type = (ValueType)0;
- if (GlobalVariables.ContainsKey(variableName))
- type = GlobalVariables[variableName].Type;
- else if (LocalVariables.ContainsKey(variableName))
- type = LocalVariables[variableName].Type;
- else if (ConstantDefinitions.Any(x => x.Name.Equals(variableName, StringComparison.OrdinalIgnoreCase)))
- type = ConstantDefinitions.First(x => x.Name.Equals(variableName, StringComparison.OrdinalIgnoreCase)).ValueType;
- var node = new ExecutionNode
- {
- Type = "assignment",
- Symbol = CurrentPosition
- };
- var variable = GetVariable(out error);
- if (error != null)
- return null;
- if (GetNextToken() != Token.Equal
- && Enumerator.Current != Token.Increment
- && Enumerator.Current != Token.Decrement
- && !Enumerator.Current.IsArithmetic())
- {
- error = new ParserError($"Unexpected token, expecting assignment: {Enumerator.Current}", CurrentPosition);
- return null;
- }
- ExecutionNode value;
- if (Enumerator.Current == Token.Increment)
- {
- value = OperateNodes(variable, CreateConstant(1, CurrentPosition), Token.Plus);
- }
- else if (Enumerator.Current == Token.Decrement)
- {
- value = OperateNodes(variable, CreateConstant(1, CurrentPosition), Token.Minus);
- }
- else if (Enumerator.Current != Token.Equal)
- {
- Token arithmeticToken = Enumerator.Current;
- if (GetNextToken() != Token.Equal)
- {
- error = new ParserError($"Unexpected token, expecting assignment: {Enumerator.Current}", CurrentPosition);
- return null;
- }
- ExecutionNode newValue = Expression(out error);
- value = OperateNodes(variable, newValue, arithmeticToken);
- }
- else
- {
- value = type == ValueType.String
- ? ParseString(out error, true, true)
- : Expression(out error);
- }
- if (error != null)
- return null;
- node.SubNodes = new[]
- {
- variable,
- new ExecutionNode
- {
- Type = "value",
- SubNodes = new[] { value }
- }
- };
- return node;
- }
- else if (Lexer.Identifier.Equals("CASE", StringComparison.OrdinalIgnoreCase))
- {
- var node = new ExecutionNode
- {
- Type = "case",
- Symbol = CurrentPosition
- };
- List<ExecutionNode> subNodes = new List<ExecutionNode>();
- do
- {
- if (GetNextToken(true) == Token.NewLine
- || GetNextToken(true) == Token.EOF)
- break;
- var value = Expression(out error);
- if (error != null)
- return null;
- if (Enumerator.Current == Token.To)
- {
- var value2 = Expression(out error);
- if (error != null)
- return null;
- subNodes.Add(new ExecutionNode
- {
- Type = "case-to",
- SubNodes = new[] { value, value2 }
- });
- continue;
- }
- subNodes.Add(new ExecutionNode
- {
- Type = "case-exact",
- SubNodes = new[] { value }
- });
- } while (Enumerator.Current == Token.Comma);
- if (Enumerator.Current != Token.NewLine
- && Enumerator.Current != Token.EOF)
- {
- error = new ParserError($"Unexpected token: {Enumerator.Current}", CurrentPosition);
- return null;
- }
- node.SubNodes = subNodes.ToArray();
- return node;
- }
- else if (Lexer.Identifier.Equals("CALL", StringComparison.OrdinalIgnoreCase)
- || Lexer.Identifier.Equals("TRYCALL", StringComparison.OrdinalIgnoreCase))
- {
- Enumerator.MoveNext();
- if (Enumerator.Current != Token.Identifer)
- {
- error = new ParserError($"Expecting a call to a function, got token instead: {Enumerator.Current}", CurrentPosition);
- return null;
- }
- Marker symbolMarker = CurrentPosition;
- string target = Lexer.Identifier;
- List<ExecutionNode> parameters = new List<ExecutionNode>();
- if (ProcedureDefinitions.All(x => !x.Name.Equals(target, StringComparison.OrdinalIgnoreCase)))
- {
- error = new ParserError($"Could not find procedure: {Lexer.Identifier}", CurrentPosition);
- return null;
- }
- Enumerator.MoveNext();
- while (Enumerator.Current != Token.NewLine
- && Enumerator.Current != Token.EOF
- && Enumerator.Current != Token.RParen)
- {
- parameters.Add(Expression(out error));
- if (error != null)
- {
- error = new ParserError($"{error.ErrorMessage} (target [{target}])", error.SymbolMarker);
- return null;
- }
- if (Enumerator.Current != Token.Comma
- && Enumerator.Current != Token.RParen
- && Enumerator.Current != Token.NewLine
- && Enumerator.Current != Token.EOF)
- {
- error = new ParserError($"Unexpected token: {Enumerator.Current}", CurrentPosition);
- return null;
- }
- }
- if (Enumerator.Current == Token.RParen)
- Enumerator.MoveNext();
- if (Enumerator.Current != Token.NewLine
- && Enumerator.Current != Token.EOF)
- {
- error = new ParserError($"Unexpected token: {Enumerator.Current}", CurrentPosition);
- return null;
- }
- return CallMethod(target, symbolMarker, parameters.ToArray());
- }
- else if (Lexer.Identifier.Equals("CALLFORM", StringComparison.OrdinalIgnoreCase)
- || Lexer.Identifier.Equals("TRYCALLFORM", StringComparison.OrdinalIgnoreCase)
- || Lexer.Identifier.Equals("TRYCCALLFORM", StringComparison.OrdinalIgnoreCase)
- || Lexer.Identifier.Equals("TRYJUMPFORM", StringComparison.OrdinalIgnoreCase))
- {
- string statementName = Lexer.Identifier;
- var node = new ExecutionNode
- {
- Type = "callform",
- Metadata =
- {
- ["try"] = statementName.StartsWith("TRY").ToString()
- },
- Symbol = CurrentPosition
- };
- ExecutionNode nameValue = null;
- List<ExecutionNode> parameters = new List<ExecutionNode>();
- Enumerator.MoveNext();
- do
- {
- ExecutionNode newValue = null;
- if (Enumerator.Current == Token.Identifer)
- {
- newValue = CreateConstant(Lexer.Identifier, CurrentPosition);
- }
- else if (Enumerator.Current == Token.OpenBracket)
- {
- newValue = Expression(out error);
- if (error != null)
- return null;
- }
- else if (Enumerator.Current == Token.LParen)
- {
- break;
- }
- else
- {
- error = new ParserError($"Unexpected token: {Enumerator.Current}", CurrentPosition);
- return null;
- }
- nameValue = nameValue == null
- ? newValue
- : OperateNodes(nameValue, newValue, Token.Plus);
- Enumerator.MoveNext();
- } while (Enumerator.Current != Token.Comma
- && Enumerator.Current != Token.NewLine
- && Enumerator.Current != Token.EOF);
- while (Enumerator.Current != Token.NewLine
- && Enumerator.Current != Token.EOF
- && Enumerator.Current != Token.RParen)
- {
- parameters.Add(Expression(out error));
- if (error != null)
- {
- error = new ParserError($"{error.ErrorMessage} (statement [{statementName}])", error.SymbolMarker);
- return null;
- }
- if (Enumerator.Current != Token.Comma
- && Enumerator.Current != Token.NewLine
- && Enumerator.Current != Token.EOF
- && Enumerator.Current != Token.RParen)
- {
- error = new ParserError($"Unexpected token: {Enumerator.Current}", CurrentPosition);
- return null;
- }
- }
- node.SubNodes = new[]
- {
- new ExecutionNode
- {
- Type = "name",
- SubNodes = new[] { nameValue }
- },
- new ExecutionNode
- {
- Type = "parameters",
- SubNodes = parameters.ToArray()
- },
- };
- return node;
- }
- else if (Lexer.Identifier.Equals("BEGIN", StringComparison.OrdinalIgnoreCase))
- {
- var node = new ExecutionNode
- {
- Type = "statement",
- Metadata =
- {
- ["name"] = "BEGIN"
- },
- Symbol = CurrentPosition
- };
- Enumerator.MoveNext();
- if (Enumerator.Current != Token.Identifer)
- {
- error = new ParserError($"Unexpected token: {Enumerator.Current}", CurrentPosition);
- return null;
- }
- node.SubNodes = new[] { CreateConstant(Lexer.Identifier, CurrentPosition) };
- return node;
- }
- else //treat as statement
- {
- string statementName = Lexer.Identifier;
- var node = new ExecutionNode
- {
- Type = "statement",
- Metadata =
- {
- ["name"] = statementName
- },
- Symbol = CurrentPosition
- };
- List<ExecutionNode> parameters = new List<ExecutionNode>();
- Keyword keyword = ExplicitKeywords.FirstOrDefault(x => x.Name == statementName);
- if (keyword?.ImplicitString == true)
- {
- var value = ParseString(out error, true, keyword.ImplicitFormatted);
- if (error != null)
- return null;
- if (value != null)
- parameters.Add(value);
- node.SubNodes = parameters.ToArray();
- return node;
- }
- if (GetNextToken(true) == Token.NewLine
- || GetNextToken(true) == Token.EOF)
- {
- return node;
- }
- if (GetNextToken(true) == Token.Colon
- || GetNextToken(true) == Token.Equal)
- {
- error = new ParserError($"Undeclared variable: {statementName}", node.Symbol);
- return null;
- }
- while (Enumerator.Current != Token.NewLine
- && Enumerator.Current != Token.EOF)
- {
- parameters.Add(Expression(out error));
- if (error != null)
- {
- error = new ParserError($"{error.ErrorMessage} (statement [{statementName}])", error.SymbolMarker);
- return null;
- }
- if (Enumerator.Current != Token.Comma
- && Enumerator.Current != Token.NewLine
- && Enumerator.Current != Token.EOF)
- {
- error = new ParserError($"Unexpected token: {Enumerator.Current}", CurrentPosition);
- return null;
- }
- }
- node.SubNodes = parameters.ToArray();
- return node;
- }
- case Token.AtSymbol:
- case Token.Sharp:
- while (Enumerator.MoveNext()
- && Enumerator.Current != Token.NewLine
- && Enumerator.Current != Token.EOF)
- {
- }
- return null;
- case Token.NewLine:
- case Token.EOF:
- return null;
- default:
- error = new ParserError($"Unexpected token: {Enumerator.Current}", CurrentPosition);
- return null;
- }
- }
- protected ExecutionNode GetVariable(out ParserError error)
- {
- string variableName = Lexer.Identifier;
- var node = new ExecutionNode
- {
- Type = "variable",
- Metadata =
- {
- ["name"] = variableName
- },
- Symbol = CurrentPosition
- };
- List<ExecutionNode> indices = new List<ExecutionNode>();
- error = null;
- while (GetNextToken(true) == Token.Colon)
- {
- GetNextToken();
- var token = GetNextToken();
- if (token == Token.LParen)
- {
- indices.Add(Expression(out error));
- if (error != null)
- return null;
- if (Enumerator.Current != Token.RParen)
- {
- error = new ParserError("Invalid expression - Expected right bracket", CurrentPosition);
- return null;
- }
- }
- else if (token == Token.Value)
- {
- indices.Add(CreateConstant(Lexer.Value, CurrentPosition));
- }
- else if (token == Token.Identifer)
- {
- if (CsvDefinition.VariableIndexDictionary.TryGetValue(variableName, out var varTable)
- && varTable.TryGetValue(Lexer.Identifier, out int index))
- {
- indices.Add(CreateConstant(index, CurrentPosition));
- continue;
- }
- if (GlobalVariables.ContainsKey(Lexer.Identifier)
- || LocalVariables.ContainsKey(Lexer.Identifier)
- || ConstantDefinitions.Any(x => x.Name == Lexer.Identifier))
- {
- var subNode = new ExecutionNode
- {
- Type = "variable",
- Metadata =
- {
- ["name"] = Lexer.Identifier
- },
- Symbol = CurrentPosition
- };
- indices.Add(subNode);
- continue;
- }
- if (FunctionDefinitions.Any(x => x.Name == Lexer.Identifier))
- {
- indices.Add(GetFunction(out error));
- if (error != null)
- return null;
- continue;
- }
- error = new ParserError($"Unknown identifier: {Lexer.Identifier}", CurrentPosition);
- return null;
- }
- }
- if (indices.Count > 0)
- {
- ExecutionNode indexNode = new ExecutionNode
- {
- Type = "index",
- SubNodes = indices.ToArray()
- };
- node.SubNodes = new[] { indexNode };
- }
- return node;
- }
- protected ExecutionNode GetFunction(out ParserError error)
- {
- error = null;
- Marker symbolMarker = CurrentPosition;
- List<ExecutionNode> parameters = new List<ExecutionNode>();
- string functionName = Lexer.Identifier;
- if (GetNextToken() != Token.LParen)
- {
- error = new ParserError($"Unexpected token: {Enumerator.Current}", CurrentPosition);
- return null;
- }
- while (Enumerator.Current == Token.Comma
- || Enumerator.Current == Token.LParen)
- {
- if (GetNextToken(true) == Token.RParen)
- break;
- if (GetNextToken(true) == Token.Comma)
- {
- var defaultValue = new ExecutionNode
- {
- Type = "defaultvalue",
- Symbol = CurrentPosition
- };
- parameters.Add(defaultValue);
- GetNextToken();
- continue;
- }
- parameters.Add(Expression(out error));
- if (error != null)
- return null;
- if (Enumerator.Current != Token.Comma
- && Enumerator.Current != Token.RParen)
- {
- error = new ParserError($"Unexpected token: {Enumerator.Current}", CurrentPosition);
- return null;
- }
- }
- if (Enumerator.Current != Token.RParen)
- {
- error = new ParserError($"Unexpected token: {Enumerator.Current}", CurrentPosition);
- return null;
- }
- if (hasPeeked)
- {
- GetNextToken();
- }
- var functionDefinition = FunctionDefinitions.FirstOrDefault(x => x.Name == functionName
- && (x.Parameters.Length >= parameters.Count
- || x.Parameters.Any(y => y.IsArrayParameter)));
- if (functionDefinition == null)
- {
- error = new ParserError($"No matching method with same amount of parameters: {functionName} ({parameters.Count})", CurrentPosition);
- return null;
- }
- return CallMethod(functionName, symbolMarker, parameters.ToArray());
- }
- private static readonly Dictionary<Token, int> OrderOfOps = new Dictionary<Token, int>
- {
- { Token.Or, 0 }, { Token.And, 0 }, { Token.Not, 0 },
- { Token.Equal, 1 }, { Token.NotEqual, 1 },
- { Token.Less, 1 }, { Token.More, 1 }, { Token.LessEqual, 1 }, { Token.MoreEqual, 1 },
- { Token.Plus, 2 }, { Token.Minus, 2 },
- { Token.Asterisk, 3 }, { Token.Slash, 3 }, { Token.Modulo, 3 },
- { Token.Caret, 4 }
- };
- protected ExecutionNode Expression(out ParserError error, bool useModulo = true, bool ternaryString = false)
- {
- error = null;
- var operators = new Stack<Token>();
- var operands = new Stack<ExecutionNode>();
- Token token;
- void ProcessOperation(out ParserError localError)
- {
- localError = null;
- Token op = operators.Pop();
- if (op.IsUnary() && operands.Count >= 1)
- {
- var operand = operands.Pop();
- operands.Push(new ExecutionNode
- {
- Type = "operation",
- Metadata =
- {
- ["type"] = GetOperationName(op),
- ["unary"] = "true"
- },
- SubNodes = new[]
- {
- operand
- }
- });
- }
- else if (operands.Count >= 2)
- {
- ExecutionNode right = operands.Pop();
- ExecutionNode left = operands.Pop();
- operands.Push(new ExecutionNode
- {
- Type = "operation",
- Metadata =
- {
- ["type"] = GetOperationName(op),
- ["unary"] = "false"
- },
- SubNodes = new[]
- {
- left,
- right
- }
- });
- }
- else
- localError = new ParserError("Invalid expression - not enough operands", CurrentPosition);
- }
- void AttemptUnaryConversion(out ParserError localError)
- {
- localError = null;
- while (operators.Count > 0
- && operators.Peek().IsUnary())
- {
- ProcessOperation(out localError);
- if (localError != null)
- return;
- }
- }
- while ((token = GetNextToken()) != Token.NewLine
- && token != Token.EOF
- && token != Token.Comma
- && token != Token.Colon
- && token != Token.To
- && token != Token.CloseBracket
- && token != Token.RParen
- && token != Token.QuestionMark
- && token != Token.Sharp
- && (!ternaryString || token != Token.TernaryEscape)
- && (useModulo || token != Token.Modulo))
- {
- if (token == Token.Value)
- {
- operands.Push(CreateConstant(Lexer.Value, CurrentPosition));
- AttemptUnaryConversion(out error);
- if (error != null)
- return null;
- }
- else if (token == Token.QuotationMark || token == Token.AtSymbol)
- {
- operands.Push(ParseString(out error, false, false));
- if (error != null)
- return null;
- }
- else if (token == Token.Identifer)
- {
- if (GlobalVariables.ContainsKey(Lexer.Identifier)
- || LocalVariables.ContainsKey(Lexer.Identifier)
- || ConstantDefinitions.Any(x => x.Name == Lexer.Identifier))
- {
- operands.Push(GetVariable(out error));
- if (error != null)
- return null;
- }
- else if (FunctionDefinitions.Any(x => x.Name == Lexer.Identifier))
- {
- operands.Push(GetFunction(out error));
- if (error != null)
- return null;
- }
- else
- {
- Warnings.Add(new ParserError($"Unknown identifier: {Lexer.Identifier}", CurrentPosition));
- break;
- }
- }
- else if (token == Token.TernaryEscape)
- {
- operands.Push(Expression(out error, useModulo, true));
- if (error != null)
- return null;
- }
- else if (token.IsArithmetic())
- {
- if (token.IsUnary())
- {
- operators.Push(token);
- continue;
- }
- if (!operands.Any() && !token.IsUnary())
- {
- error = new ParserError($"Invalid unary operator: {token}", CurrentPosition);
- return null;
- }
- while (operators.Any() && OrderOfOps[token] <= OrderOfOps[operators.Peek()])
- {
- ProcessOperation(out error);
- if (error != null)
- return null;
- }
- operators.Push(token);
- }
- else if (token == Token.LParen)
- {
- operands.Push(Expression(out var localError));
- if (localError != null)
- {
- error = localError;
- return null;
- }
- }
- else if (token == Token.RParen)
- {
- break;
- }
- else
- {
- error = new ParserError($"Unexpected token: {token}", CurrentPosition);
- return null;
- }
- }
- while (operators.Any())
- {
- ProcessOperation(out error);
- if (error != null)
- return null;
- }
- if (!operands.Any())
- {
- error = new ParserError("Invalid expression - Empty operand stack", CurrentPosition);
- return null;
- }
- var result = operands.Pop();
- if (token != Token.QuestionMark)
- return result;
- var resultTrue = ternaryString ? ParseString(out error, useModulo, true, true) : Expression(out error, useModulo, false);
- if (error != null)
- return null;
- var resultFalse = ternaryString ? ParseString(out error, useModulo, true, true) : Expression(out error, useModulo, false);
- if (error != null)
- return null;
- return CallMethod("__INLINEIF", CurrentPosition, result, resultTrue, resultFalse);
- }
- protected ExecutionNode ParseString(out ParserError error, bool implicitString, bool canFormat = false, bool nestedTernary = false)
- {
- error = null;
- ExecutionNode value = null;
- if (Lexer.IsPeeking)
- Lexer.GetNextChar();
- if (nestedTernary && (Lexer.CurrentChar == '?' || Lexer.CurrentChar == '#'))
- Lexer.GetNextChar();
- if (!implicitString)
- {
- if (char.IsWhiteSpace(Lexer.CurrentChar))
- Lexer.GetNextChar();
- if (Lexer.CurrentChar == '@')
- {
- canFormat = true;
- Lexer.GetNextChar();
- }
- if (Lexer.CurrentChar == '"')
- {
- Lexer.GetNextChar();
- }
- }
- StringBuilder currentBlock = new StringBuilder();
- while ((Lexer.CurrentChar != '"' || implicitString)
- && Lexer.CurrentChar != '\n'
- && Lexer.CurrentChar != '\0')
- {
- if (Lexer.CurrentChar == '\r')
- {
- Lexer.GetNextChar();
- continue;
- }
- if (nestedTernary && Lexer.CurrentChar == '#')
- break;
- if (canFormat && Lexer.CurrentChar == '\\')
- {
- Lexer.GetNextChar();
- if (Lexer.CurrentChar == '@')
- {
- if (nestedTernary)
- break;
- var expressionValue = Expression(out error, true, true);
- if (error != null)
- return null;
- value = value == null
- ? expressionValue
- : OperateNodes(value, expressionValue, Token.Plus);
- }
- currentBlock.Append(Lexer.CurrentChar);
- Lexer.GetNextChar();
- continue;
- }
- if (canFormat && (Lexer.CurrentChar == '{' || Lexer.CurrentChar == '%'))
- {
- bool useModulo = Lexer.CurrentChar != '%';
- List<ExecutionNode> formatParams = new List<ExecutionNode>();
- Marker symbolMarker = CurrentPosition;
- do
- {
- var expressionValue = Expression(out error, useModulo, nestedTernary);
- if (error != null)
- return null;
- formatParams.Add(expressionValue);
- } while (Enumerator.Current == Token.Comma);
- var formattedValue = CallMethod("__FORMAT", symbolMarker, formatParams.ToArray());
- value = value == null
- ? formattedValue
- : OperateNodes(value, formattedValue, Token.Plus);
- Lexer.GetNextChar();
- continue;
- }
- currentBlock.Append(Lexer.CurrentChar);
- Lexer.GetNextChar();
- }
- if (!nestedTernary && !implicitString && (Lexer.CurrentChar == '\0' || Lexer.CurrentChar == '\n'))
- {
- error = new ParserError("Was expecting string to be closed", CurrentPosition);
- return null;
- }
- ExecutionNode appendedValue = CreateConstant(currentBlock.ToString(), CurrentPosition);
- value = value == null
- ? appendedValue
- : OperateNodes(value, appendedValue, Token.Plus);
- return value;
- }
- private static readonly Dictionary<Token, string> OperationNames = new Dictionary<Token, string>
- {
- [Token.Plus] = "add",
- [Token.Asterisk] = "multiply",
- [Token.Minus] = "subtract",
- [Token.Slash] = "divide",
- };
- public static string GetOperationName(Token token)
- {
- return OperationNames.TryGetValue(token, out string result)
- ? result
- : token.ToString();
- }
- public static ExecutionNode CreateConstant(Value value, Marker symbolMarker)
- {
- return new ExecutionNode
- {
- Type = "constant",
- Metadata =
- {
- ["type"] = value.Type.ToString(),
- ["value"] = value.ToString()
- },
- Symbol = symbolMarker
- };
- }
- public static ExecutionNode OperateNodes(ExecutionNode left, ExecutionNode right, Token token)
- {
- return new ExecutionNode
- {
- Type = "operation",
- Metadata =
- {
- ["type"] = GetOperationName(token)
- },
- SubNodes = new[]
- {
- left,
- right
- }
- };
- }
- public static ExecutionNode CallMethod(string methodName, Marker symbolMarker, params ExecutionNode[] parameters)
- {
- return new ExecutionNode
- {
- Type = "call",
- Metadata =
- {
- ["target"] = methodName
- },
- Symbol = symbolMarker,
- SubNodes = new[]
- {
- new ExecutionNode
- {
- Type = "parameters",
- SubNodes = parameters.ToArray()
- }
- }
- };
- }
- }
- }
|