Parser.cs 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace NTERA.Interpreter.Compiler
  6. {
  7. public class Parser
  8. {
  9. protected Lexer Lexer { get; }
  10. protected FunctionDefinition SelfDefinition { get; }
  11. protected ICollection<FunctionDefinition> FunctionDefinitions { get; }
  12. protected ICollection<FunctionDefinition> ProcedureDefinitions { get; }
  13. protected List<ParserError> Errors { get; } = new List<ParserError>();
  14. protected List<ParserError> Warnings { get; } = new List<ParserError>();
  15. protected VariableDictionary GlobalVariables { get; }
  16. protected VariableDictionary LocalVariables { get; }
  17. protected ICollection<Keyword> ExplicitKeywords { get; }
  18. protected CSVDefinition CsvDefinition { get; }
  19. protected IEnumerator<Token> Enumerator { get; }
  20. protected bool hasPeeked = false;
  21. protected Token peekedToken = Token.Unknown;
  22. protected Token GetNextToken(bool peek = false)
  23. {
  24. if (peek && hasPeeked)
  25. return peekedToken;
  26. if (!hasPeeked)
  27. Enumerator.MoveNext();
  28. peekedToken = Enumerator.Current;
  29. hasPeeked = peek;
  30. return Enumerator.Current;
  31. }
  32. protected Marker CurrentPosition => new Marker(Lexer.TokenMarker.Pointer + SelfDefinition.Position.Pointer,
  33. Lexer.TokenMarker.Line + SelfDefinition.Position.Line - 1,
  34. Lexer.TokenMarker.Column);
  35. public Parser(string input, FunctionDefinition selfDefinition, ICollection<FunctionDefinition> functionDefinitions, ICollection<FunctionDefinition> procedureDefinitions, VariableDictionary globalVariables, VariableDictionary localVariables, ICollection<Keyword> explicitKeywords, CSVDefinition csvDefinition)
  36. {
  37. Lexer = new Lexer(input);
  38. Enumerator = Lexer.GetEnumerator();
  39. SelfDefinition = selfDefinition;
  40. FunctionDefinitions = functionDefinitions;
  41. ProcedureDefinitions = procedureDefinitions;
  42. GlobalVariables = globalVariables;
  43. LocalVariables = localVariables;
  44. ExplicitKeywords = explicitKeywords;
  45. CsvDefinition = csvDefinition;
  46. }
  47. public IEnumerable<ExecutionNode> Parse(out List<ParserError> errors, out List<ParserError> warnings)
  48. {
  49. List<ExecutionNode> nodes = new List<ExecutionNode>();
  50. using (Enumerator)
  51. {
  52. do
  53. {
  54. var node = ParseLine(out var error);
  55. if (error != null)
  56. {
  57. Errors.Add(error);
  58. nodes.Add(new ExecutionNode
  59. {
  60. Type = "error",
  61. Metadata =
  62. {
  63. ["message"] = error.ErrorMessage,
  64. ["symbol"] = error.SymbolMarker.ToString()
  65. },
  66. Symbol = error.SymbolMarker
  67. });
  68. //resynchronize to a new line
  69. while (Enumerator.MoveNext()
  70. && Enumerator.Current != Token.NewLine
  71. && Enumerator.Current != Token.EOF)
  72. {
  73. }
  74. }
  75. else if (node != null)
  76. {
  77. nodes.Add(node);
  78. }
  79. hasPeeked = false;
  80. } while (Enumerator.MoveNext());
  81. }
  82. errors = Errors;
  83. warnings = Warnings;
  84. return nodes;
  85. }
  86. protected ExecutionNode ParseLine(out ParserError error)
  87. {
  88. error = null;
  89. switch (Enumerator.Current)
  90. {
  91. case Token.Identifer:
  92. if (GlobalVariables.ContainsKey(Lexer.Identifier)
  93. || LocalVariables.ContainsKey(Lexer.Identifier))
  94. {
  95. string variableName = Lexer.Identifier;
  96. bool isGlobal = GlobalVariables.ContainsKey(variableName);
  97. var node = new ExecutionNode
  98. {
  99. Type = "assignment",
  100. Symbol = CurrentPosition
  101. };
  102. var variable = GetVariable(out error);
  103. if (error != null)
  104. return null;
  105. if (GetNextToken() != Token.Equal
  106. && Enumerator.Current != Token.Increment
  107. && Enumerator.Current != Token.Decrement
  108. && !Enumerator.Current.IsArithmetic())
  109. {
  110. error = new ParserError($"Unexpected token, expecting assignment: {Enumerator.Current}", CurrentPosition);
  111. return null;
  112. }
  113. ExecutionNode value;
  114. if (Enumerator.Current == Token.Increment)
  115. {
  116. value = OperateNodes(variable, CreateConstant(1), Token.Plus);
  117. }
  118. else if (Enumerator.Current == Token.Decrement)
  119. {
  120. value = OperateNodes(variable, CreateConstant(1), Token.Minus);
  121. }
  122. else if (Enumerator.Current != Token.Equal)
  123. {
  124. Token arithmeticToken = Enumerator.Current;
  125. if (GetNextToken() != Token.Equal)
  126. {
  127. error = new ParserError($"Unexpected token, expecting assignment: {Enumerator.Current}", CurrentPosition);
  128. return null;
  129. }
  130. ExecutionNode newValue = Expression(out error);
  131. value = OperateNodes(variable, newValue, arithmeticToken);
  132. }
  133. else
  134. {
  135. var type = isGlobal
  136. ? GlobalVariables[variableName].Type
  137. : LocalVariables[variableName].Type;
  138. value = type == ValueType.String
  139. ? ParseString(out error, true, true)
  140. : Expression(out error);
  141. }
  142. if (error != null)
  143. return null;
  144. node.SubNodes = new[]
  145. {
  146. variable,
  147. new ExecutionNode
  148. {
  149. Type = "value",
  150. SubNodes = new[] { value }
  151. }
  152. };
  153. return node;
  154. }
  155. else if (Lexer.Identifier.Equals("CASE", StringComparison.OrdinalIgnoreCase))
  156. {
  157. var node = new ExecutionNode
  158. {
  159. Type = "case",
  160. Symbol = CurrentPosition
  161. };
  162. List<ExecutionNode> subNodes = new List<ExecutionNode>();
  163. do
  164. {
  165. if (GetNextToken(true) == Token.NewLine
  166. || GetNextToken(true) == Token.EOF)
  167. break;
  168. var value = Expression(out error);
  169. if (error != null)
  170. return null;
  171. if (Enumerator.Current == Token.To)
  172. {
  173. var value2 = Expression(out error);
  174. if (error != null)
  175. return null;
  176. subNodes.Add(new ExecutionNode
  177. {
  178. Type = "case-to",
  179. SubNodes = new[] { value, value2 }
  180. });
  181. continue;
  182. }
  183. subNodes.Add(new ExecutionNode
  184. {
  185. Type = "case-exact",
  186. SubNodes = new[] { value }
  187. });
  188. } while (Enumerator.Current == Token.Comma);
  189. if (Enumerator.Current != Token.NewLine
  190. && Enumerator.Current != Token.EOF)
  191. {
  192. error = new ParserError($"Unexpected token: {Enumerator.Current}", CurrentPosition);
  193. return null;
  194. }
  195. node.SubNodes = subNodes.ToArray();
  196. return node;
  197. }
  198. else if (Lexer.Identifier.Equals("CALL", StringComparison.OrdinalIgnoreCase)
  199. || Lexer.Identifier.Equals("TRYCALL", StringComparison.OrdinalIgnoreCase))
  200. {
  201. Enumerator.MoveNext();
  202. if (Enumerator.Current != Token.Identifer)
  203. {
  204. error = new ParserError($"Expecting a call to a function, got token instead: {Enumerator.Current}", CurrentPosition);
  205. return null;
  206. }
  207. Marker symbolMarker = CurrentPosition;
  208. string target = Lexer.Identifier;
  209. List<ExecutionNode> parameters = new List<ExecutionNode>();
  210. if (ProcedureDefinitions.All(x => !x.Name.Equals(target, StringComparison.OrdinalIgnoreCase)))
  211. {
  212. error = new ParserError($"Could not find procedure: {Lexer.Identifier}", CurrentPosition);
  213. return null;
  214. }
  215. Enumerator.MoveNext();
  216. while (Enumerator.Current != Token.NewLine
  217. && Enumerator.Current != Token.EOF
  218. && Enumerator.Current != Token.RParen)
  219. {
  220. parameters.Add(Expression(out error));
  221. if (error != null)
  222. {
  223. error = new ParserError($"{error.ErrorMessage} (target [{target}])", error.SymbolMarker);
  224. return null;
  225. }
  226. if (Enumerator.Current != Token.Comma
  227. && Enumerator.Current != Token.RParen
  228. && Enumerator.Current != Token.NewLine
  229. && Enumerator.Current != Token.EOF)
  230. {
  231. error = new ParserError($"Unexpected token: {Enumerator.Current}", CurrentPosition);
  232. return null;
  233. }
  234. }
  235. if (Enumerator.Current == Token.RParen)
  236. Enumerator.MoveNext();
  237. if (Enumerator.Current != Token.NewLine
  238. && Enumerator.Current != Token.EOF)
  239. {
  240. error = new ParserError($"Unexpected token: {Enumerator.Current}", CurrentPosition);
  241. return null;
  242. }
  243. return CallMethod(target, symbolMarker, parameters.ToArray());
  244. }
  245. else if (Lexer.Identifier.Equals("CALLFORM", StringComparison.OrdinalIgnoreCase)
  246. || Lexer.Identifier.Equals("TRYCALLFORM", StringComparison.OrdinalIgnoreCase)
  247. || Lexer.Identifier.Equals("TRYJUMPFORM", StringComparison.OrdinalIgnoreCase))
  248. {
  249. string statementName = Lexer.Identifier;
  250. var node = new ExecutionNode
  251. {
  252. Type = "callform",
  253. Metadata =
  254. {
  255. ["try"] = statementName.StartsWith("TRY").ToString()
  256. },
  257. Symbol = CurrentPosition
  258. };
  259. ExecutionNode nameValue = null;
  260. List<ExecutionNode> parameters = new List<ExecutionNode>();
  261. Enumerator.MoveNext();
  262. do
  263. {
  264. ExecutionNode newValue = null;
  265. if (Enumerator.Current == Token.Identifer)
  266. {
  267. newValue = CreateConstant(Lexer.Identifier);
  268. }
  269. else if (Enumerator.Current == Token.OpenBracket)
  270. {
  271. newValue = Expression(out error);
  272. if (error != null)
  273. return null;
  274. }
  275. else if (Enumerator.Current == Token.LParen)
  276. {
  277. break;
  278. }
  279. else
  280. {
  281. error = new ParserError($"Unexpected token: {Enumerator.Current}", CurrentPosition);
  282. return null;
  283. }
  284. nameValue = nameValue == null
  285. ? newValue
  286. : OperateNodes(nameValue, newValue, Token.Plus);
  287. Enumerator.MoveNext();
  288. } while (Enumerator.Current != Token.Comma
  289. && Enumerator.Current != Token.NewLine
  290. && Enumerator.Current != Token.EOF);
  291. while (Enumerator.Current != Token.NewLine
  292. && Enumerator.Current != Token.EOF
  293. && Enumerator.Current != Token.RParen)
  294. {
  295. parameters.Add(Expression(out error));
  296. if (error != null)
  297. {
  298. error = new ParserError($"{error.ErrorMessage} (statement [{statementName}])", error.SymbolMarker);
  299. return null;
  300. }
  301. if (Enumerator.Current != Token.Comma
  302. && Enumerator.Current != Token.NewLine
  303. && Enumerator.Current != Token.EOF
  304. && Enumerator.Current != Token.RParen)
  305. {
  306. error = new ParserError($"Unexpected token: {Enumerator.Current}", CurrentPosition);
  307. return null;
  308. }
  309. }
  310. node.SubNodes = new[]
  311. {
  312. new ExecutionNode
  313. {
  314. Type = "name",
  315. SubNodes = new[] { nameValue }
  316. },
  317. new ExecutionNode
  318. {
  319. Type = "parameters",
  320. SubNodes = parameters.ToArray()
  321. },
  322. };
  323. return node;
  324. }
  325. else if (Lexer.Identifier.Equals("BEGIN", StringComparison.OrdinalIgnoreCase))
  326. {
  327. var node = new ExecutionNode
  328. {
  329. Type = "statement",
  330. Metadata =
  331. {
  332. ["name"] = "BEGIN"
  333. },
  334. Symbol = CurrentPosition
  335. };
  336. Enumerator.MoveNext();
  337. if (Enumerator.Current != Token.Identifer)
  338. {
  339. error = new ParserError($"Unexpected token: {Enumerator.Current}", CurrentPosition);
  340. return null;
  341. }
  342. node.SubNodes = new[] { CreateConstant(Lexer.Identifier) };
  343. return node;
  344. }
  345. else //treat as statement
  346. {
  347. string statementName = Lexer.Identifier;
  348. var node = new ExecutionNode
  349. {
  350. Type = "statement",
  351. Metadata =
  352. {
  353. ["name"] = statementName
  354. },
  355. Symbol = CurrentPosition
  356. };
  357. List<ExecutionNode> parameters = new List<ExecutionNode>();
  358. Keyword keyword = ExplicitKeywords.FirstOrDefault(x => x.Name == statementName);
  359. if (keyword?.ImplicitString == true)
  360. {
  361. var value = ParseString(out error, true, keyword.ImplicitFormatted);
  362. if (error != null)
  363. return null;
  364. if (value != null)
  365. parameters.Add(value);
  366. node.SubNodes = parameters.ToArray();
  367. return node;
  368. }
  369. if (GetNextToken(true) == Token.NewLine
  370. || GetNextToken(true) == Token.EOF)
  371. {
  372. return node;
  373. }
  374. if (GetNextToken(true) == Token.Colon
  375. || GetNextToken(true) == Token.Equal)
  376. {
  377. error = new ParserError($"Undeclared variable: {statementName}", node.Symbol);
  378. return null;
  379. }
  380. while (Enumerator.Current != Token.NewLine
  381. && Enumerator.Current != Token.EOF)
  382. {
  383. parameters.Add(Expression(out error));
  384. if (error != null)
  385. {
  386. error = new ParserError($"{error.ErrorMessage} (statement [{statementName}])", error.SymbolMarker);
  387. return null;
  388. }
  389. if (Enumerator.Current != Token.Comma
  390. && Enumerator.Current != Token.NewLine
  391. && Enumerator.Current != Token.EOF)
  392. {
  393. error = new ParserError($"Unexpected token: {Enumerator.Current}", CurrentPosition);
  394. return null;
  395. }
  396. }
  397. node.SubNodes = parameters.ToArray();
  398. return node;
  399. }
  400. case Token.AtSymbol:
  401. case Token.Sharp:
  402. while (Enumerator.MoveNext()
  403. && Enumerator.Current != Token.NewLine
  404. && Enumerator.Current != Token.EOF)
  405. {
  406. }
  407. return null;
  408. case Token.NewLine:
  409. case Token.EOF:
  410. return null;
  411. default:
  412. error = new ParserError($"Unexpected token: {Enumerator.Current}", CurrentPosition);
  413. return null;
  414. }
  415. }
  416. protected ExecutionNode GetVariable(out ParserError error)
  417. {
  418. string variableName = Lexer.Identifier;
  419. var node = new ExecutionNode
  420. {
  421. Type = "variable",
  422. Metadata =
  423. {
  424. ["name"] = variableName
  425. },
  426. Symbol = CurrentPosition
  427. };
  428. List<ExecutionNode> indices = new List<ExecutionNode>();
  429. error = null;
  430. while (GetNextToken(true) == Token.Colon)
  431. {
  432. GetNextToken();
  433. var token = GetNextToken();
  434. if (token == Token.LParen)
  435. {
  436. indices.Add(Expression(out error));
  437. if (error != null)
  438. return null;
  439. if (Enumerator.Current != Token.RParen)
  440. {
  441. error = new ParserError("Invalid expression - Expected right bracket", CurrentPosition);
  442. return null;
  443. }
  444. }
  445. else if (token == Token.Value)
  446. {
  447. indices.Add(CreateConstant(Lexer.Value));
  448. }
  449. else if (token == Token.Identifer)
  450. {
  451. if (CsvDefinition.VariableIndexDictionary.TryGetValue(variableName, out var varTable)
  452. && varTable.TryGetValue(Lexer.Identifier, out int index))
  453. {
  454. indices.Add(CreateConstant(index));
  455. continue;
  456. }
  457. if (GlobalVariables.ContainsKey(Lexer.Identifier)
  458. || LocalVariables.ContainsKey(Lexer.Identifier))
  459. {
  460. var subNode = new ExecutionNode
  461. {
  462. Type = "variable",
  463. Metadata =
  464. {
  465. ["name"] = Lexer.Identifier
  466. },
  467. Symbol = CurrentPosition
  468. };
  469. indices.Add(subNode);
  470. continue;
  471. }
  472. if (FunctionDefinitions.Any(x => x.Name == Lexer.Identifier))
  473. {
  474. indices.Add(GetFunction(out error));
  475. if (error != null)
  476. return null;
  477. continue;
  478. }
  479. error = new ParserError($"Unknown identifier: {Lexer.Identifier}", CurrentPosition);
  480. return null;
  481. }
  482. }
  483. if (indices.Count > 0)
  484. {
  485. ExecutionNode indexNode = new ExecutionNode
  486. {
  487. Type = "index",
  488. SubNodes = indices.ToArray()
  489. };
  490. node.SubNodes = new[] { indexNode };
  491. }
  492. return node;
  493. }
  494. protected ExecutionNode GetFunction(out ParserError error)
  495. {
  496. error = null;
  497. Marker symbolMarker = CurrentPosition;
  498. List<ExecutionNode> parameters = new List<ExecutionNode>();
  499. string functionName = Lexer.Identifier;
  500. if (GetNextToken() != Token.LParen)
  501. {
  502. error = new ParserError($"Unexpected token: {Enumerator.Current}", CurrentPosition);
  503. return null;
  504. }
  505. while (Enumerator.Current == Token.Comma
  506. || Enumerator.Current == Token.LParen)
  507. {
  508. if (GetNextToken(true) == Token.RParen)
  509. break;
  510. if (GetNextToken(true) == Token.Comma)
  511. {
  512. var defaultValue = new ExecutionNode
  513. {
  514. Type = "defaultvalue",
  515. Symbol = CurrentPosition
  516. };
  517. parameters.Add(defaultValue);
  518. GetNextToken();
  519. continue;
  520. }
  521. parameters.Add(Expression(out error));
  522. if (error != null)
  523. return null;
  524. if (Enumerator.Current != Token.Comma
  525. && Enumerator.Current != Token.RParen)
  526. {
  527. error = new ParserError($"Unexpected token: {Enumerator.Current}", CurrentPosition);
  528. return null;
  529. }
  530. }
  531. if (Enumerator.Current != Token.RParen)
  532. {
  533. error = new ParserError($"Unexpected token: {Enumerator.Current}", CurrentPosition);
  534. return null;
  535. }
  536. if (hasPeeked)
  537. {
  538. GetNextToken();
  539. }
  540. var functionDefinition = FunctionDefinitions.FirstOrDefault(x => x.Name == functionName
  541. && (x.Parameters.Length >= parameters.Count
  542. || x.Parameters.Any(y => y.IsArrayParameter)));
  543. if (functionDefinition == null)
  544. {
  545. error = new ParserError($"No matching method with same amount of parameters: {functionName} ({parameters.Count})", CurrentPosition);
  546. return null;
  547. }
  548. return CallMethod(functionName, symbolMarker, parameters.ToArray());
  549. }
  550. private static readonly Dictionary<Token, int> OrderOfOps = new Dictionary<Token, int>
  551. {
  552. { Token.Or, 0 }, { Token.And, 0 }, { Token.Not, 0 },
  553. { Token.Equal, 1 }, { Token.NotEqual, 1 },
  554. { Token.Less, 1 }, { Token.More, 1 }, { Token.LessEqual, 1 }, { Token.MoreEqual, 1 },
  555. { Token.Plus, 2 }, { Token.Minus, 2 },
  556. { Token.Asterisk, 3 }, { Token.Slash, 3 }, { Token.Modulo, 3 },
  557. { Token.Caret, 4 }
  558. };
  559. protected ExecutionNode Expression(out ParserError error, bool useModulo = true, bool ternaryString = false)
  560. {
  561. error = null;
  562. var operators = new Stack<Token>();
  563. var operands = new Stack<ExecutionNode>();
  564. Token token;
  565. void ProcessOperation(out ParserError localError)
  566. {
  567. localError = null;
  568. Token op = operators.Pop();
  569. if (op.IsUnary() && operands.Count >= 1)
  570. {
  571. var operand = operands.Pop();
  572. operands.Push(new ExecutionNode
  573. {
  574. Type = "operation",
  575. Metadata =
  576. {
  577. ["type"] = GetOperationName(op),
  578. ["unary"] = "true"
  579. },
  580. SubNodes = new[]
  581. {
  582. operand
  583. }
  584. });
  585. }
  586. else if (operands.Count >= 2)
  587. {
  588. ExecutionNode right = operands.Pop();
  589. ExecutionNode left = operands.Pop();
  590. operands.Push(new ExecutionNode
  591. {
  592. Type = "operation",
  593. Metadata =
  594. {
  595. ["type"] = GetOperationName(op),
  596. ["unary"] = "false"
  597. },
  598. SubNodes = new[]
  599. {
  600. left,
  601. right
  602. }
  603. });
  604. }
  605. else
  606. localError = new ParserError("Invalid expression - not enough operands", CurrentPosition);
  607. }
  608. void AttemptUnaryConversion(out ParserError localError)
  609. {
  610. localError = null;
  611. while (operators.Count > 0
  612. && operators.Peek().IsUnary())
  613. {
  614. ProcessOperation(out localError);
  615. if (localError != null)
  616. return;
  617. }
  618. }
  619. while ((token = GetNextToken()) != Token.NewLine
  620. && token != Token.EOF
  621. && token != Token.Comma
  622. && token != Token.Colon
  623. && token != Token.To
  624. && token != Token.CloseBracket
  625. && token != Token.RParen
  626. && token != Token.QuestionMark
  627. && token != Token.Sharp
  628. && (!ternaryString || token != Token.TernaryEscape)
  629. && (useModulo || token != Token.Modulo))
  630. {
  631. if (token == Token.Value)
  632. {
  633. operands.Push(CreateConstant(Lexer.Value));
  634. AttemptUnaryConversion(out error);
  635. if (error != null)
  636. return null;
  637. }
  638. else if (token == Token.QuotationMark || token == Token.AtSymbol)
  639. {
  640. operands.Push(ParseString(out error, false, false));
  641. if (error != null)
  642. return null;
  643. }
  644. else if (token == Token.Identifer)
  645. {
  646. if (GlobalVariables.ContainsKey(Lexer.Identifier)
  647. || LocalVariables.ContainsKey(Lexer.Identifier))
  648. {
  649. operands.Push(GetVariable(out error));
  650. if (error != null)
  651. return null;
  652. }
  653. else if (FunctionDefinitions.Any(x => x.Name == Lexer.Identifier))
  654. {
  655. operands.Push(GetFunction(out error));
  656. if (error != null)
  657. return null;
  658. }
  659. else
  660. {
  661. Warnings.Add(new ParserError($"Unknown identifier: {Lexer.Identifier}", CurrentPosition));
  662. break;
  663. }
  664. }
  665. else if (token == Token.TernaryEscape)
  666. {
  667. operands.Push(Expression(out error, useModulo, true));
  668. if (error != null)
  669. return null;
  670. }
  671. else if (token.IsArithmetic())
  672. {
  673. if (token.IsUnary())
  674. {
  675. operators.Push(token);
  676. continue;
  677. }
  678. if (!operands.Any() && !token.IsUnary())
  679. {
  680. error = new ParserError($"Invalid unary operator: {token}", CurrentPosition);
  681. return null;
  682. }
  683. while (operators.Any() && OrderOfOps[token] <= OrderOfOps[operators.Peek()])
  684. {
  685. ProcessOperation(out error);
  686. if (error != null)
  687. return null;
  688. }
  689. operators.Push(token);
  690. }
  691. else if (token == Token.LParen)
  692. {
  693. operands.Push(Expression(out var localError));
  694. if (localError != null)
  695. {
  696. error = localError;
  697. return null;
  698. }
  699. }
  700. else if (token == Token.RParen)
  701. {
  702. break;
  703. }
  704. else
  705. {
  706. error = new ParserError($"Unexpected token: {token}", CurrentPosition);
  707. return null;
  708. }
  709. }
  710. while (operators.Any())
  711. {
  712. ProcessOperation(out error);
  713. if (error != null)
  714. return null;
  715. }
  716. if (!operands.Any())
  717. {
  718. error = new ParserError("Invalid expression - Empty operand stack", CurrentPosition);
  719. return null;
  720. }
  721. var result = operands.Pop();
  722. if (token != Token.QuestionMark)
  723. return result;
  724. var resultTrue = ternaryString ? ParseString(out error, useModulo, true, true) : Expression(out error, useModulo, false);
  725. if (error != null)
  726. return null;
  727. var resultFalse = ternaryString ? ParseString(out error, useModulo, true, true) : Expression(out error, useModulo, false);
  728. if (error != null)
  729. return null;
  730. return CallMethod("__IMPLICITIF", CurrentPosition, result, resultTrue, resultFalse);
  731. }
  732. protected ExecutionNode ParseString(out ParserError error, bool implicitString, bool canFormat = false, bool nestedTernary = false)
  733. {
  734. error = null;
  735. ExecutionNode value = null;
  736. if (Lexer.IsPeeking)
  737. Lexer.GetNextChar();
  738. if (nestedTernary && (Lexer.CurrentChar == '?' || Lexer.CurrentChar == '#'))
  739. Lexer.GetNextChar();
  740. if (!implicitString)
  741. {
  742. if (char.IsWhiteSpace(Lexer.CurrentChar))
  743. Lexer.GetNextChar();
  744. if (Lexer.CurrentChar == '@')
  745. {
  746. canFormat = true;
  747. Lexer.GetNextChar();
  748. }
  749. if (Lexer.CurrentChar == '"')
  750. {
  751. Lexer.GetNextChar();
  752. }
  753. }
  754. StringBuilder currentBlock = new StringBuilder();
  755. while ((Lexer.CurrentChar != '"' || implicitString)
  756. && Lexer.CurrentChar != '\n'
  757. && Lexer.CurrentChar != '\0')
  758. {
  759. if (Lexer.CurrentChar == '\r')
  760. {
  761. Lexer.GetNextChar();
  762. continue;
  763. }
  764. if (nestedTernary && Lexer.CurrentChar == '#')
  765. break;
  766. if (canFormat && Lexer.CurrentChar == '\\')
  767. {
  768. Lexer.GetNextChar();
  769. if (Lexer.CurrentChar == '@')
  770. {
  771. if (nestedTernary)
  772. break;
  773. var expressionValue = Expression(out error, true, true);
  774. if (error != null)
  775. return null;
  776. value = value == null
  777. ? expressionValue
  778. : OperateNodes(value, expressionValue, Token.Plus);
  779. }
  780. currentBlock.Append(Lexer.CurrentChar);
  781. Lexer.GetNextChar();
  782. continue;
  783. }
  784. if (canFormat && (Lexer.CurrentChar == '{' || Lexer.CurrentChar == '%'))
  785. {
  786. bool useModulo = Lexer.CurrentChar != '%';
  787. List<ExecutionNode> formatParams = new List<ExecutionNode>();
  788. Marker symbolMarker = CurrentPosition;
  789. do
  790. {
  791. var expressionValue = Expression(out error, useModulo, nestedTernary);
  792. if (error != null)
  793. return null;
  794. formatParams.Add(expressionValue);
  795. } while (Enumerator.Current == Token.Comma);
  796. var formattedValue = CallMethod("__FORMAT", symbolMarker, formatParams.ToArray());
  797. value = value == null
  798. ? formattedValue
  799. : OperateNodes(value, formattedValue, Token.Plus);
  800. Lexer.GetNextChar();
  801. continue;
  802. }
  803. currentBlock.Append(Lexer.CurrentChar);
  804. Lexer.GetNextChar();
  805. }
  806. if (!nestedTernary && !implicitString && (Lexer.CurrentChar == '\0' || Lexer.CurrentChar == '\n'))
  807. {
  808. error = new ParserError("Was expecting string to be closed", CurrentPosition);
  809. return null;
  810. }
  811. ExecutionNode appendedValue = CreateConstant(currentBlock.ToString());
  812. value = value == null
  813. ? appendedValue
  814. : OperateNodes(value, appendedValue, Token.Plus);
  815. return value;
  816. }
  817. private static readonly Dictionary<Token, string> OperationNames = new Dictionary<Token, string>
  818. {
  819. [Token.Plus] = "add",
  820. [Token.Asterisk] = "multiply",
  821. [Token.Minus] = "subtract",
  822. [Token.Slash] = "divide",
  823. };
  824. private static string GetOperationName(Token token)
  825. {
  826. return OperationNames.TryGetValue(token, out string result)
  827. ? result
  828. : token.ToString();
  829. }
  830. private ExecutionNode CreateConstant(Value value)
  831. {
  832. return new ExecutionNode
  833. {
  834. Type = "constant",
  835. Metadata =
  836. {
  837. ["type"] = value.Type.ToString(),
  838. ["value"] = value.ToString()
  839. },
  840. Symbol = CurrentPosition
  841. };
  842. }
  843. private static ExecutionNode OperateNodes(ExecutionNode left, ExecutionNode right, Token token)
  844. {
  845. return new ExecutionNode
  846. {
  847. Type = "operation",
  848. Metadata =
  849. {
  850. ["type"] = GetOperationName(token)
  851. },
  852. SubNodes = new[]
  853. {
  854. left,
  855. right
  856. }
  857. };
  858. }
  859. private static ExecutionNode CallMethod(string methodName, Marker symbolMarker, params ExecutionNode[] parameters)
  860. {
  861. return new ExecutionNode
  862. {
  863. Type = "call",
  864. Metadata =
  865. {
  866. ["target"] = methodName
  867. },
  868. Symbol = symbolMarker,
  869. SubNodes = new[]
  870. {
  871. new ExecutionNode
  872. {
  873. Type = "parameters",
  874. SubNodes = parameters.ToArray()
  875. }
  876. }
  877. };
  878. }
  879. }
  880. }