PageRenderTime 56ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/node_modules/jsonpath/node_modules/jison/node_modules/lex-parser/tests/all-tests.js

https://gitlab.com/jonnyforney/beerbowerpainting
JavaScript | 396 lines | 327 code | 69 blank | 0 comment | 1 complexity | ca4946668fc3f8a35edf992f02716f50 MD5 | raw file
  1. var assert = require("assert"),
  2. lex = require("../lex-parser"),
  3. fs = require('fs'),
  4. path = require('path');
  5. function read (p, file) {
  6. return fs.readFileSync(path.join(__dirname, p, file), "utf8");
  7. }
  8. exports["test lex grammar with macros"] = function () {
  9. var lexgrammar = 'D [0-9]\nID [a-zA-Z][a-zA-Z0-9]+\n%%\n\n{D}"ohhai" {print(9);}\n"{" return \'{\';';
  10. var expected = {
  11. macros: {"D": "[0-9]", "ID": "[a-zA-Z][a-zA-Z0-9]+"},
  12. rules: [
  13. ["{D}ohhai\\b", "print(9);"],
  14. ["\\{", "return '{';"]
  15. ]
  16. };
  17. assert.deepEqual(lex.parse(lexgrammar), expected, "grammar should be parsed correctly");
  18. };
  19. exports["test escaped chars"] = function () {
  20. var lexgrammar = '%%\n"\\n"+ {return \'NL\';}\n\\n+ {return \'NL2\';}\n\\s+ {/* skip */}';
  21. var expected = {
  22. rules: [
  23. ["\\\\n+", "return 'NL';"],
  24. ["\\n+", "return 'NL2';"],
  25. ["\\s+", "/* skip */"]
  26. ]
  27. };
  28. assert.deepEqual(lex.parse(lexgrammar), expected, "grammar should be parsed correctly");
  29. };
  30. exports["test advanced"] = function () {
  31. var lexgrammar = '%%\n$ {return \'EOF\';}\n. {/* skip */}\n"stuff"*/("{"|";") {/* ok */}\n(.+)[a-z]{1,2}"hi"*? {/* skip */}\n';
  32. var expected = {
  33. rules: [
  34. ["$", "return 'EOF';"],
  35. [".", "/* skip */"],
  36. ["stuff*(?=(\\{|;))", "/* ok */"],
  37. ["(.+)[a-z]{1,2}hi*?", "/* skip */"]
  38. ]
  39. };
  40. assert.deepEqual(lex.parse(lexgrammar), expected, "grammar should be parsed correctly");
  41. };
  42. exports["test [^\\]]"] = function () {
  43. var lexgrammar = '%%\n"["[^\\]]"]" {return true;}\n\'f"oo\\\'bar\' {return \'baz2\';}\n"fo\\"obar" {return \'baz\';}\n';
  44. var expected = {
  45. rules: [
  46. ["\\[[^\\]]\\]", "return true;"],
  47. ["f\"oo'bar\\b", "return 'baz2';"],
  48. ['fo"obar\\b', "return 'baz';"]
  49. ]
  50. };
  51. assert.deepEqual(lex.parse(lexgrammar), expected, "grammar should be parsed correctly");
  52. };
  53. exports["test multiline action"] = function () {
  54. var lexgrammar = '%%\n"["[^\\]]"]" %{\nreturn true;\n%}\n';
  55. var expected = {
  56. rules: [
  57. ["\\[[^\\]]\\]", "\nreturn true;\n"]
  58. ]
  59. };
  60. assert.deepEqual(lex.parse(lexgrammar), expected, "grammar should be parsed correctly");
  61. };
  62. exports["test multiline action with single braces"] = function () {
  63. var lexgrammar = '%%\n"["[^\\]]"]" {\nvar b={};return true;\n}\n';
  64. var expected = {
  65. rules: [
  66. ["\\[[^\\]]\\]", "\nvar b={};return true;\n"]
  67. ]
  68. };
  69. assert.deepEqual(lex.parse(lexgrammar), expected, "grammar should be parsed correctly");
  70. };
  71. exports["test multiline action with brace in a multi-line-comment"] = function () {
  72. var lexgrammar = '%%\n"["[^\\]]"]" {\nvar b={}; /* { */ return true;\n}\n';
  73. var expected = {
  74. rules: [
  75. ["\\[[^\\]]\\]", "\nvar b={}; /* { */ return true;\n"]
  76. ]
  77. };
  78. assert.deepEqual(lex.parse(lexgrammar), expected, "grammar should be parsed correctly");
  79. };
  80. exports["test multiline action with brace in a single-line-comment"] = function () {
  81. var lexgrammar = '%%\n"["[^\\]]"]" {\nvar b={}; // { \nreturn 2 / 3;\n}\n';
  82. var expected = {
  83. rules: [
  84. ["\\[[^\\]]\\]", "\nvar b={}; // { \nreturn 2 / 3;\n"]
  85. ]
  86. };
  87. assert.deepEqual(lex.parse(lexgrammar), expected, "grammar should be parsed correctly");
  88. };
  89. exports["test multiline action with braces in strings"] = function () {
  90. var lexgrammar = '%%\n"["[^\\]]"]" {\nvar b=\'{\' + "{"; // { \nreturn 2 / 3;\n}\n';
  91. var expected = {
  92. rules: [
  93. ["\\[[^\\]]\\]", "\nvar b='{' + \"{\"; // { \nreturn 2 / 3;\n"]
  94. ]
  95. };
  96. assert.deepEqual(lex.parse(lexgrammar), expected, "grammar should be parsed correctly");
  97. };
  98. exports["test multiline action with braces in regexp"] = function () {
  99. var lexgrammar = '%%\n"["[^\\]]"]" {\nvar b=/{/; // { \nreturn 2 / 3;\n}\n';
  100. var expected = {
  101. rules: [
  102. ["\\[[^\\]]\\]", "\nvar b=/{/; // { \nreturn 2 / 3;\n"]
  103. ]
  104. };
  105. assert.deepEqual(lex.parse(lexgrammar), expected, "grammar should be parsed correctly");
  106. };
  107. exports["test include"] = function () {
  108. var lexgrammar = '\nRULE [0-9]\n\n%{\n hi <stuff> \n%}\n%%\n"["[^\\]]"]" %{\nreturn true;\n%}\n';
  109. var expected = {
  110. macros: {"RULE": "[0-9]"},
  111. actionInclude: "\n hi <stuff> \n",
  112. rules: [
  113. ["\\[[^\\]]\\]", "\nreturn true;\n"]
  114. ]
  115. };
  116. assert.deepEqual(lex.parse(lexgrammar), expected, "grammar should be parsed correctly");
  117. };
  118. exports["test bnf lex grammar"] = function () {
  119. var lexgrammar = lex.parse(read('lex', 'bnf.jisonlex'));
  120. var expected = JSON.parse(read('lex', 'bnf.lex.json'));
  121. assert.deepEqual(lexgrammar, expected, "grammar should be parsed correctly");
  122. };
  123. exports["test lex grammar bootstrap"] = function () {
  124. var lexgrammar = lex.parse(read('lex', 'lex_grammar.jisonlex'));
  125. var expected = JSON.parse(read('lex', 'lex_grammar.lex.json'));
  126. assert.deepEqual(lexgrammar, expected, "grammar should be parsed correctly");
  127. };
  128. exports["test ANSI C lexical grammar"] = function () {
  129. var lexgrammar = lex.parse(read('lex','ansic.jisonlex'));
  130. assert.ok(lexgrammar, "grammar should be parsed correctly");
  131. };
  132. exports["test advanced"] = function () {
  133. var lexgrammar = '%%\n"stuff"*/!("{"|";") {/* ok */}\n';
  134. var expected = {
  135. rules: [
  136. ["stuff*(?!(\\{|;))", "/* ok */"],
  137. ]
  138. };
  139. assert.deepEqual(lex.parse(lexgrammar), expected, "grammar should be parsed correctly");
  140. };
  141. exports["test start conditions"] = function () {
  142. var lexgrammar = '%s TEST TEST2\n%x EAT\n%%\n'+
  143. '"enter-test" {this.begin(\'TEST\');}\n'+
  144. '<TEST,EAT>"x" {return \'T\';}\n'+
  145. '<*>"z" {return \'Z\';}\n'+
  146. '<TEST>"y" {this.begin(\'INITIAL\'); return \'TY\';}';
  147. var expected = {
  148. startConditions: {
  149. "TEST": 0,
  150. "TEST2": 0,
  151. "EAT": 1,
  152. },
  153. rules: [
  154. ["enter-test\\b", "this.begin('TEST');" ],
  155. [["TEST","EAT"], "x\\b", "return 'T';" ],
  156. [["*"], "z\\b", "return 'Z';" ],
  157. [["TEST"], "y\\b", "this.begin('INITIAL'); return 'TY';" ]
  158. ]
  159. };
  160. assert.deepEqual(lex.parse(lexgrammar), expected, "grammar should be parsed correctly");
  161. };
  162. exports["test no brace action"] = function () {
  163. var lexgrammar = '%%\n"["[^\\]]"]" return true;\n"x" return 1;';
  164. var expected = {
  165. rules: [
  166. ["\\[[^\\]]\\]", "return true;"],
  167. ["x\\b", "return 1;"]
  168. ]
  169. };
  170. assert.deepEqual(lex.parse(lexgrammar), expected, "grammar should be parsed correctly");
  171. };
  172. exports["test quote escape"] = function () {
  173. var lexgrammar = '%%\n\\"\\\'"x" return 1;';
  174. var expected = {
  175. rules: [
  176. ["\"'x\\b", "return 1;"]
  177. ]
  178. };
  179. assert.deepEqual(lex.parse(lexgrammar), expected, "grammar should be parsed correctly");
  180. };
  181. exports["test escape things"] = function () {
  182. var lexgrammar = '%%\n\\"\\\'\\\\\\*\\i return 1;\n"a"\\b return 2;\n\\cA {}\n\\012 {}\n\\xFF {}';
  183. var expected = {
  184. rules: [
  185. ["\"'\\\\\\*i\\b", "return 1;"],
  186. ["a\\b", "return 2;"],
  187. ["\\cA", ""],
  188. ["\\012", ""],
  189. ["\\xFF", ""]
  190. ]
  191. };
  192. assert.deepEqual(lex.parse(lexgrammar), expected, "grammar should be parsed correctly");
  193. };
  194. exports["test unicode encoding"] = function () {
  195. var lexgrammar = '%%\n"\\u03c0" return 1;';
  196. var expected = {
  197. rules: [
  198. ["\\u03c0", "return 1;"]
  199. ]
  200. };
  201. assert.deepEqual(lex.parse(lexgrammar), expected, "grammar should be parsed correctly");
  202. };
  203. exports["test unicode"] = function () {
  204. var lexgrammar = '%%\n"π" return 1;';
  205. var expected = {
  206. rules: [
  207. ["π", "return 1;"]
  208. ]
  209. };
  210. assert.deepEqual(lex.parse(lexgrammar), expected, "grammar should be parsed correctly");
  211. };
  212. exports["test bugs"] = function () {
  213. var lexgrammar = '%%\n\\\'([^\\\\\']+|\\\\(\\n|.))*?\\\' return 1;';
  214. var expected = {
  215. rules: [
  216. ["'([^\\\\']+|\\\\(\\n|.))*?'", "return 1;"]
  217. ]
  218. };
  219. assert.deepEqual(lex.parse(lexgrammar), expected, "grammar should be parsed correctly");
  220. };
  221. exports["test special groupings"] = function () {
  222. var lexgrammar = '%%\n(?:"foo"|"bar")\\(\\) return 1;';
  223. var expected = {
  224. rules: [
  225. ["(?:foo|bar)\\(\\)", "return 1;"]
  226. ]
  227. };
  228. assert.deepEqual(lex.parse(lexgrammar), expected, "grammar should be parsed correctly");
  229. };
  230. exports["test trailing code include"] = function () {
  231. var lexgrammar = '%%"foo" {return bar;}\n%% var bar = 1;';
  232. var expected = {
  233. rules: [
  234. ['foo\\b', "return bar;"]
  235. ],
  236. moduleInclude: " var bar = 1;"
  237. };
  238. assert.deepEqual(lex.parse(lexgrammar), expected, "grammar should be parsed correctly");
  239. };
  240. exports["test empty or regex"] = function () {
  241. var lexgrammar = '%%\n(|"bar")("foo"|)(|) return 1;';
  242. var expected = {
  243. rules: [
  244. ["(|bar)(foo|)(|)", "return 1;"]
  245. ]
  246. };
  247. assert.deepEqual(lex.parse(lexgrammar), expected, "grammar should be parsed correctly");
  248. };
  249. exports["test options"] = function () {
  250. var lexgrammar = '%options flex\n%%\n"foo" return 1;';
  251. var expected = {
  252. rules: [
  253. ["foo", "return 1;"]
  254. ],
  255. options: {flex: true}
  256. };
  257. assert.deepEqual(lex.parse(lexgrammar), expected, "grammar should be parsed correctly");
  258. };
  259. exports["test unquoted string rules"] = function () {
  260. var lexgrammar = "%%\nfoo* return 1";
  261. var expected = {
  262. rules: [
  263. ["foo*", "return 1"]
  264. ]
  265. };
  266. assert.deepEqual(lex.parse(lexgrammar), expected, "grammar should be parsed correctly");
  267. };
  268. exports["test [^\\\\]"] = function () {
  269. var lexgrammar = '%%\n"["[^\\\\]"]" {return true;}\n\'f"oo\\\'bar\' {return \'baz2\';}\n"fo\\"obar" {return \'baz\';}\n';
  270. var expected = {
  271. rules: [
  272. ["\\[[^\\\\]\\]", "return true;"],
  273. ["f\"oo'bar\\b", "return 'baz2';"],
  274. ['fo"obar\\b', "return 'baz';"]
  275. ]
  276. };
  277. assert.deepEqual(lex.parse(lexgrammar), expected, "grammar should be parsed correctly");
  278. };
  279. exports["test comments"] = function () {
  280. var lexgrammar = "/* */ // foo\n%%\nfoo* return 1";
  281. var expected = {
  282. rules: [
  283. ["foo*", "return 1"]
  284. ]
  285. };
  286. assert.deepEqual(lex.parse(lexgrammar), expected, "grammar should be parsed correctly");
  287. };
  288. exports["test rules with trailing escapes"] = function () {
  289. var lexgrammar = '%%\n\\#[^\\n]*\\n {/* ok */}\n';
  290. var expected = {
  291. rules: [
  292. ["#[^\\n]*\\n", "/* ok */"],
  293. ]
  294. };
  295. assert.deepEqual(lex.parse(lexgrammar), expected, "grammar should be parsed correctly");
  296. };
  297. exports["test no brace action with surplus whitespace between rules"] = function () {
  298. var lexgrammar = '%%\n"a" return true;\n \n"b" return 1;\n \n';
  299. var expected = {
  300. rules: [
  301. ["a\\b", "return true;"],
  302. ["b\\b", "return 1;"]
  303. ]
  304. };
  305. assert.deepEqual(lex.parse(lexgrammar), expected, "grammar should be parsed correctly");
  306. };
  307. exports["test windows line endings"] = function () {
  308. var lexgrammar = '%%\r\n"["[^\\]]"]" %{\r\nreturn true;\r\n%}\r\n';
  309. var expected = {
  310. rules: [
  311. ["\\[[^\\]]\\]", "\r\nreturn true;\r\n"]
  312. ]
  313. };
  314. assert.deepEqual(lex.parse(lexgrammar), expected, "grammar should be parsed correctly");
  315. };
  316. exports["test braced action with surplus whitespace between rules"] = function () {
  317. var lexgrammar = '%%\n"a" %{ \nreturn true;\n%} \n \n"b" %{ return 1;\n%} \n \n';
  318. var expected = {
  319. rules: [
  320. ["a\\b", " \nreturn true;\n"],
  321. ["b\\b", " return 1;\n"]
  322. ]
  323. };
  324. assert.deepEqual(lex.parse(lexgrammar), expected, "grammar should be parsed correctly");
  325. };
  326. if (require.main === module)
  327. require("test").run(exports);