lexer_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. package parser
  2. import (
  3. "testing"
  4. "github.com/dop251/goja/file"
  5. "github.com/dop251/goja/token"
  6. "github.com/dop251/goja/unistring"
  7. )
  8. func TestLexer(t *testing.T) {
  9. tt(t, func() {
  10. setup := func(src string) *_parser {
  11. parser := newParser("", src)
  12. return parser
  13. }
  14. test := func(src string, test ...interface{}) {
  15. parser := setup(src)
  16. for len(test) > 0 {
  17. tkn, literal, _, idx := parser.scan()
  18. if len(test) > 0 {
  19. is(tkn, test[0].(token.Token))
  20. test = test[1:]
  21. }
  22. if len(test) > 0 {
  23. is(literal, unistring.String(test[0].(string)))
  24. test = test[1:]
  25. }
  26. if len(test) > 0 {
  27. // FIXME terst, Fix this so that cast to file.Idx is not necessary?
  28. is(idx, file.Idx(test[0].(int)))
  29. test = test[1:]
  30. }
  31. }
  32. }
  33. test("",
  34. token.EOF, "", 1,
  35. )
  36. test("#!",
  37. token.EOF, "", 3,
  38. )
  39. test("#!\n1",
  40. token.NUMBER, "1", 4,
  41. token.EOF, "", 5,
  42. )
  43. test("1",
  44. token.NUMBER, "1", 1,
  45. token.EOF, "", 2,
  46. )
  47. test(".0",
  48. token.NUMBER, ".0", 1,
  49. token.EOF, "", 3,
  50. )
  51. test("abc",
  52. token.IDENTIFIER, "abc", 1,
  53. token.EOF, "", 4,
  54. )
  55. test("abc(1)",
  56. token.IDENTIFIER, "abc", 1,
  57. token.LEFT_PARENTHESIS, "", 4,
  58. token.NUMBER, "1", 5,
  59. token.RIGHT_PARENTHESIS, "", 6,
  60. token.EOF, "", 7,
  61. )
  62. test(".",
  63. token.PERIOD, "", 1,
  64. token.EOF, "", 2,
  65. )
  66. test("===.",
  67. token.STRICT_EQUAL, "", 1,
  68. token.PERIOD, "", 4,
  69. token.EOF, "", 5,
  70. )
  71. test(">>>=.0",
  72. token.UNSIGNED_SHIFT_RIGHT_ASSIGN, "", 1,
  73. token.NUMBER, ".0", 5,
  74. token.EOF, "", 7,
  75. )
  76. test(">>>=0.0.",
  77. token.UNSIGNED_SHIFT_RIGHT_ASSIGN, "", 1,
  78. token.NUMBER, "0.0", 5,
  79. token.PERIOD, "", 8,
  80. token.EOF, "", 9,
  81. )
  82. test("\"abc\"",
  83. token.STRING, "\"abc\"", 1,
  84. token.EOF, "", 6,
  85. )
  86. test("abc = //",
  87. token.IDENTIFIER, "abc", 1,
  88. token.ASSIGN, "", 5,
  89. token.EOF, "", 9,
  90. )
  91. test("abc = 1 / 2",
  92. token.IDENTIFIER, "abc", 1,
  93. token.ASSIGN, "", 5,
  94. token.NUMBER, "1", 7,
  95. token.SLASH, "", 9,
  96. token.NUMBER, "2", 11,
  97. token.EOF, "", 12,
  98. )
  99. test("xyzzy = 'Nothing happens.'",
  100. token.IDENTIFIER, "xyzzy", 1,
  101. token.ASSIGN, "", 7,
  102. token.STRING, "'Nothing happens.'", 9,
  103. token.EOF, "", 27,
  104. )
  105. test("abc = !false",
  106. token.IDENTIFIER, "abc", 1,
  107. token.ASSIGN, "", 5,
  108. token.NOT, "", 7,
  109. token.BOOLEAN, "false", 8,
  110. token.EOF, "", 13,
  111. )
  112. test("abc = !!true",
  113. token.IDENTIFIER, "abc", 1,
  114. token.ASSIGN, "", 5,
  115. token.NOT, "", 7,
  116. token.NOT, "", 8,
  117. token.BOOLEAN, "true", 9,
  118. token.EOF, "", 13,
  119. )
  120. test("abc *= 1",
  121. token.IDENTIFIER, "abc", 1,
  122. token.MULTIPLY_ASSIGN, "", 5,
  123. token.NUMBER, "1", 8,
  124. token.EOF, "", 9,
  125. )
  126. test("if 1 else",
  127. token.IF, "if", 1,
  128. token.NUMBER, "1", 4,
  129. token.ELSE, "else", 6,
  130. token.EOF, "", 10,
  131. )
  132. test("null",
  133. token.NULL, "null", 1,
  134. token.EOF, "", 5,
  135. )
  136. test(`"\u007a\x79\u000a\x78"`,
  137. token.STRING, "\"\\u007a\\x79\\u000a\\x78\"", 1,
  138. token.EOF, "", 23,
  139. )
  140. test(`"[First line \
  141. Second line \
  142. Third line\
  143. . ]"
  144. `,
  145. token.STRING, "\"[First line \\\nSecond line \\\n Third line\\\n. ]\"", 1,
  146. token.EOF, "", 53,
  147. )
  148. test("/",
  149. token.SLASH, "", 1,
  150. token.EOF, "", 2,
  151. )
  152. test("var abc = \"abc\uFFFFabc\"",
  153. token.VAR, "var", 1,
  154. token.IDENTIFIER, "abc", 5,
  155. token.ASSIGN, "", 9,
  156. token.STRING, "\"abc\uFFFFabc\"", 11,
  157. token.EOF, "", 22,
  158. )
  159. test(`'\t' === '\r'`,
  160. token.STRING, "'\\t'", 1,
  161. token.STRICT_EQUAL, "", 6,
  162. token.STRING, "'\\r'", 10,
  163. token.EOF, "", 14,
  164. )
  165. test(`var \u0024 = 1`,
  166. token.VAR, "var", 1,
  167. token.IDENTIFIER, "\\u0024", 5,
  168. token.ASSIGN, "", 12,
  169. token.NUMBER, "1", 14,
  170. token.EOF, "", 15,
  171. )
  172. test("10e10000",
  173. token.NUMBER, "10e10000", 1,
  174. token.EOF, "", 9,
  175. )
  176. test(`var if var class`,
  177. token.VAR, "var", 1,
  178. token.IF, "if", 5,
  179. token.VAR, "var", 8,
  180. token.CLASS, "class", 12,
  181. token.EOF, "", 17,
  182. )
  183. test(`-0`,
  184. token.MINUS, "", 1,
  185. token.NUMBER, "0", 2,
  186. token.EOF, "", 3,
  187. )
  188. test(`.01`,
  189. token.NUMBER, ".01", 1,
  190. token.EOF, "", 4,
  191. )
  192. test(`.01e+2`,
  193. token.NUMBER, ".01e+2", 1,
  194. token.EOF, "", 7,
  195. )
  196. test(";",
  197. token.SEMICOLON, "", 1,
  198. token.EOF, "", 2,
  199. )
  200. test(";;",
  201. token.SEMICOLON, "", 1,
  202. token.SEMICOLON, "", 2,
  203. token.EOF, "", 3,
  204. )
  205. test("//",
  206. token.EOF, "", 3,
  207. )
  208. test(";;//",
  209. token.SEMICOLON, "", 1,
  210. token.SEMICOLON, "", 2,
  211. token.EOF, "", 5,
  212. )
  213. test("1",
  214. token.NUMBER, "1", 1,
  215. )
  216. test("12 123",
  217. token.NUMBER, "12", 1,
  218. token.NUMBER, "123", 4,
  219. )
  220. test("1.2 12.3",
  221. token.NUMBER, "1.2", 1,
  222. token.NUMBER, "12.3", 5,
  223. )
  224. test("1_000 1_000_000",
  225. token.NUMBER, "1_000", 1,
  226. token.NUMBER, "1_000_000", 7,
  227. )
  228. test(`1n`,
  229. token.NUMBER, "1n", 1,
  230. )
  231. test(`1n 9007199254740991n`,
  232. token.NUMBER, "1n", 1,
  233. token.NUMBER, "9007199254740991n", 4,
  234. )
  235. test(`0xabn`,
  236. token.NUMBER, "0xabn", 1,
  237. )
  238. test(`0xabcdef0123456789abcdef0123n`,
  239. token.NUMBER, "0xabcdef0123456789abcdef0123n", 1,
  240. )
  241. test("/ /=",
  242. token.SLASH, "", 1,
  243. token.QUOTIENT_ASSIGN, "", 3,
  244. )
  245. test(`"abc"`,
  246. token.STRING, `"abc"`, 1,
  247. )
  248. test(`'abc'`,
  249. token.STRING, `'abc'`, 1,
  250. )
  251. test("++",
  252. token.INCREMENT, "", 1,
  253. )
  254. test(">",
  255. token.GREATER, "", 1,
  256. )
  257. test(">=",
  258. token.GREATER_OR_EQUAL, "", 1,
  259. )
  260. test(">>",
  261. token.SHIFT_RIGHT, "", 1,
  262. )
  263. test(">>=",
  264. token.SHIFT_RIGHT_ASSIGN, "", 1,
  265. )
  266. test(">>>",
  267. token.UNSIGNED_SHIFT_RIGHT, "", 1,
  268. )
  269. test(">>>=",
  270. token.UNSIGNED_SHIFT_RIGHT_ASSIGN, "", 1,
  271. )
  272. test("1 \"abc\"",
  273. token.NUMBER, "1", 1,
  274. token.STRING, "\"abc\"", 3,
  275. )
  276. test(",",
  277. token.COMMA, "", 1,
  278. )
  279. test("1, \"abc\"",
  280. token.NUMBER, "1", 1,
  281. token.COMMA, "", 2,
  282. token.STRING, "\"abc\"", 4,
  283. )
  284. test("new abc(1, 3.14159);",
  285. token.NEW, "new", 1,
  286. token.IDENTIFIER, "abc", 5,
  287. token.LEFT_PARENTHESIS, "", 8,
  288. token.NUMBER, "1", 9,
  289. token.COMMA, "", 10,
  290. token.NUMBER, "3.14159", 12,
  291. token.RIGHT_PARENTHESIS, "", 19,
  292. token.SEMICOLON, "", 20,
  293. )
  294. test("1 == \"1\"",
  295. token.NUMBER, "1", 1,
  296. token.EQUAL, "", 3,
  297. token.STRING, "\"1\"", 6,
  298. )
  299. test("1\n[]\n",
  300. token.NUMBER, "1", 1,
  301. token.LEFT_BRACKET, "", 3,
  302. token.RIGHT_BRACKET, "", 4,
  303. )
  304. test("1\ufeff[]\ufeff",
  305. token.NUMBER, "1", 1,
  306. token.LEFT_BRACKET, "", 5,
  307. token.RIGHT_BRACKET, "", 6,
  308. )
  309. test("x ?.30 : false",
  310. token.IDENTIFIER, "x", 1,
  311. token.QUESTION_MARK, "", 3,
  312. token.NUMBER, ".30", 4,
  313. token.COLON, "", 8,
  314. token.BOOLEAN, "false", 10,
  315. )
  316. test("a\n?.b",
  317. token.IDENTIFIER, "a", 1,
  318. token.QUESTION_DOT, "", 3,
  319. token.IDENTIFIER, "b", 5,
  320. )
  321. // ILLEGAL
  322. test(`3ea`,
  323. token.ILLEGAL, "3e", 1,
  324. token.IDENTIFIER, "a", 3,
  325. token.EOF, "", 4,
  326. )
  327. test(`3in`,
  328. token.ILLEGAL, "3", 1,
  329. token.IN, "in", 2,
  330. token.EOF, "", 4,
  331. )
  332. test("\"Hello\nWorld\"",
  333. token.ILLEGAL, "", 1,
  334. token.IDENTIFIER, "World", 8,
  335. token.ILLEGAL, "", 13,
  336. token.EOF, "", 14,
  337. )
  338. test("\u203f = 10",
  339. token.ILLEGAL, "", 1,
  340. token.ASSIGN, "", 5,
  341. token.NUMBER, "10", 7,
  342. token.EOF, "", 9,
  343. )
  344. test(`"\x0G"`,
  345. token.ILLEGAL, "\"\\x0G\"", 1,
  346. //token.STRING, "\"\\x0G\"", 1,
  347. token.EOF, "", 7,
  348. )
  349. })
  350. }