Parser.cs 27 KB

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