Parser.cs 30 KB

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