parser_test.go 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239
  1. package parser_test
  2. import (
  3. "fmt"
  4. "io"
  5. "reflect"
  6. "strconv"
  7. "strings"
  8. "testing"
  9. . "github.com/d5/tengo/v2/parser"
  10. "github.com/d5/tengo/v2/require"
  11. "github.com/d5/tengo/v2/token"
  12. )
  13. func TestParserError(t *testing.T) {
  14. err := &Error{Pos: SourceFilePos{
  15. Offset: 10, Line: 1, Column: 10,
  16. }, Msg: "test"}
  17. require.Equal(t, "Parse Error: test\n\tat 1:10", err.Error())
  18. }
  19. func TestParserErrorList(t *testing.T) {
  20. var list ErrorList
  21. list.Add(SourceFilePos{Offset: 20, Line: 2, Column: 10}, "error 2")
  22. list.Add(SourceFilePos{Offset: 30, Line: 3, Column: 10}, "error 3")
  23. list.Add(SourceFilePos{Offset: 10, Line: 1, Column: 10}, "error 1")
  24. list.Sort()
  25. require.Equal(t, "Parse Error: error 1\n\tat 1:10 (and 2 more errors)",
  26. list.Error())
  27. }
  28. func TestParseArray(t *testing.T) {
  29. expectParse(t, "[1, 2, 3]", func(p pfn) []Stmt {
  30. return stmts(
  31. exprStmt(
  32. arrayLit(p(1, 1), p(1, 9),
  33. intLit(1, p(1, 2)),
  34. intLit(2, p(1, 5)),
  35. intLit(3, p(1, 8)))))
  36. })
  37. expectParse(t, `
  38. [
  39. 1,
  40. 2,
  41. 3
  42. ]`, func(p pfn) []Stmt {
  43. return stmts(
  44. exprStmt(
  45. arrayLit(p(2, 1), p(6, 1),
  46. intLit(1, p(3, 2)),
  47. intLit(2, p(4, 2)),
  48. intLit(3, p(5, 2)))))
  49. })
  50. expectParse(t, `
  51. [
  52. 1,
  53. 2,
  54. 3
  55. ]`, func(p pfn) []Stmt {
  56. return stmts(
  57. exprStmt(
  58. arrayLit(p(2, 1), p(7, 1),
  59. intLit(1, p(3, 2)),
  60. intLit(2, p(4, 2)),
  61. intLit(3, p(5, 2)))))
  62. })
  63. expectParse(t, `[1, "foo", 12.34]`, func(p pfn) []Stmt {
  64. return stmts(
  65. exprStmt(
  66. arrayLit(p(1, 1), p(1, 17),
  67. intLit(1, p(1, 2)),
  68. stringLit("foo", p(1, 5)),
  69. floatLit(12.34, p(1, 12)))))
  70. })
  71. expectParse(t, "a = [1, 2, 3]", func(p pfn) []Stmt {
  72. return stmts(
  73. assignStmt(
  74. exprs(ident("a", p(1, 1))),
  75. exprs(arrayLit(p(1, 5), p(1, 13),
  76. intLit(1, p(1, 6)),
  77. intLit(2, p(1, 9)),
  78. intLit(3, p(1, 12)))),
  79. token.Assign,
  80. p(1, 3)))
  81. })
  82. expectParse(t, "a = [1 + 2, b * 4, [4, c]]", func(p pfn) []Stmt {
  83. return stmts(
  84. assignStmt(
  85. exprs(ident("a", p(1, 1))),
  86. exprs(arrayLit(p(1, 5), p(1, 26),
  87. binaryExpr(
  88. intLit(1, p(1, 6)),
  89. intLit(2, p(1, 10)),
  90. token.Add,
  91. p(1, 8)),
  92. binaryExpr(
  93. ident("b", p(1, 13)),
  94. intLit(4, p(1, 17)),
  95. token.Mul,
  96. p(1, 15)),
  97. arrayLit(p(1, 20), p(1, 25),
  98. intLit(4, p(1, 21)),
  99. ident("c", p(1, 24))))),
  100. token.Assign,
  101. p(1, 3)))
  102. })
  103. expectParseError(t, `[1, 2, 3,]`)
  104. expectParseError(t, `
  105. [
  106. 1,
  107. 2,
  108. 3,
  109. ]`)
  110. expectParseError(t, `
  111. [
  112. 1,
  113. 2,
  114. 3,
  115. ]`)
  116. expectParseError(t, `[1, 2, 3, ,]`)
  117. }
  118. func TestParseAssignment(t *testing.T) {
  119. expectParse(t, "a = 5", func(p pfn) []Stmt {
  120. return stmts(
  121. assignStmt(
  122. exprs(ident("a", p(1, 1))),
  123. exprs(intLit(5, p(1, 5))),
  124. token.Assign,
  125. p(1, 3)))
  126. })
  127. expectParse(t, "a := 5", func(p pfn) []Stmt {
  128. return stmts(
  129. assignStmt(
  130. exprs(ident("a", p(1, 1))),
  131. exprs(intLit(5, p(1, 6))),
  132. token.Define,
  133. p(1, 3)))
  134. })
  135. expectParse(t, "a, b = 5, 10", func(p pfn) []Stmt {
  136. return stmts(
  137. assignStmt(
  138. exprs(
  139. ident("a", p(1, 1)),
  140. ident("b", p(1, 4))),
  141. exprs(
  142. intLit(5, p(1, 8)),
  143. intLit(10, p(1, 11))),
  144. token.Assign,
  145. p(1, 6)))
  146. })
  147. expectParse(t, "a, b := 5, 10", func(p pfn) []Stmt {
  148. return stmts(
  149. assignStmt(
  150. exprs(
  151. ident("a", p(1, 1)),
  152. ident("b", p(1, 4))),
  153. exprs(
  154. intLit(5, p(1, 9)),
  155. intLit(10, p(1, 12))),
  156. token.Define,
  157. p(1, 6)))
  158. })
  159. expectParse(t, "a, b = a + 2, b - 8", func(p pfn) []Stmt {
  160. return stmts(
  161. assignStmt(
  162. exprs(
  163. ident("a", p(1, 1)),
  164. ident("b", p(1, 4))),
  165. exprs(
  166. binaryExpr(
  167. ident("a", p(1, 8)),
  168. intLit(2, p(1, 12)),
  169. token.Add,
  170. p(1, 10)),
  171. binaryExpr(
  172. ident("b", p(1, 15)),
  173. intLit(8, p(1, 19)),
  174. token.Sub,
  175. p(1, 17))),
  176. token.Assign,
  177. p(1, 6)))
  178. })
  179. expectParse(t, "a = [1, 2, 3]", func(p pfn) []Stmt {
  180. return stmts(
  181. assignStmt(
  182. exprs(ident("a", p(1, 1))),
  183. exprs(arrayLit(p(1, 5), p(1, 13),
  184. intLit(1, p(1, 6)),
  185. intLit(2, p(1, 9)),
  186. intLit(3, p(1, 12)))),
  187. token.Assign,
  188. p(1, 3)))
  189. })
  190. expectParse(t, "a = [1 + 2, b * 4, [4, c]]", func(p pfn) []Stmt {
  191. return stmts(
  192. assignStmt(
  193. exprs(ident("a", p(1, 1))),
  194. exprs(arrayLit(p(1, 5), p(1, 26),
  195. binaryExpr(
  196. intLit(1, p(1, 6)),
  197. intLit(2, p(1, 10)),
  198. token.Add,
  199. p(1, 8)),
  200. binaryExpr(
  201. ident("b", p(1, 13)),
  202. intLit(4, p(1, 17)),
  203. token.Mul,
  204. p(1, 15)),
  205. arrayLit(p(1, 20), p(1, 25),
  206. intLit(4, p(1, 21)),
  207. ident("c", p(1, 24))))),
  208. token.Assign,
  209. p(1, 3)))
  210. })
  211. expectParse(t, "a += 5", func(p pfn) []Stmt {
  212. return stmts(
  213. assignStmt(
  214. exprs(ident("a", p(1, 1))),
  215. exprs(intLit(5, p(1, 6))),
  216. token.AddAssign,
  217. p(1, 3)))
  218. })
  219. expectParse(t, "a *= 5 + 10", func(p pfn) []Stmt {
  220. return stmts(
  221. assignStmt(
  222. exprs(ident("a", p(1, 1))),
  223. exprs(
  224. binaryExpr(
  225. intLit(5, p(1, 6)),
  226. intLit(10, p(1, 10)),
  227. token.Add,
  228. p(1, 8))),
  229. token.MulAssign,
  230. p(1, 3)))
  231. })
  232. }
  233. func TestParseBoolean(t *testing.T) {
  234. expectParse(t, "true", func(p pfn) []Stmt {
  235. return stmts(
  236. exprStmt(
  237. boolLit(true, p(1, 1))))
  238. })
  239. expectParse(t, "false", func(p pfn) []Stmt {
  240. return stmts(
  241. exprStmt(
  242. boolLit(false, p(1, 1))))
  243. })
  244. expectParse(t, "true != false", func(p pfn) []Stmt {
  245. return stmts(
  246. exprStmt(
  247. binaryExpr(
  248. boolLit(true, p(1, 1)),
  249. boolLit(false, p(1, 9)),
  250. token.NotEqual,
  251. p(1, 6))))
  252. })
  253. expectParse(t, "!false", func(p pfn) []Stmt {
  254. return stmts(
  255. exprStmt(
  256. unaryExpr(
  257. boolLit(false, p(1, 2)),
  258. token.Not,
  259. p(1, 1))))
  260. })
  261. }
  262. func TestParseCall(t *testing.T) {
  263. expectParse(t, "add(1, 2, 3)", func(p pfn) []Stmt {
  264. return stmts(
  265. exprStmt(
  266. callExpr(
  267. ident("add", p(1, 1)),
  268. p(1, 4), p(1, 12), NoPos,
  269. intLit(1, p(1, 5)),
  270. intLit(2, p(1, 8)),
  271. intLit(3, p(1, 11)))))
  272. })
  273. expectParse(t, "add(1, 2, v...)", func(p pfn) []Stmt {
  274. return stmts(
  275. exprStmt(
  276. callExpr(
  277. ident("add", p(1, 1)),
  278. p(1, 4), p(1, 15), p(1, 12),
  279. intLit(1, p(1, 5)),
  280. intLit(2, p(1, 8)),
  281. ident("v", p(1, 11)))))
  282. })
  283. expectParse(t, "a = add(1, 2, 3)", func(p pfn) []Stmt {
  284. return stmts(
  285. assignStmt(
  286. exprs(
  287. ident("a", p(1, 1))),
  288. exprs(
  289. callExpr(
  290. ident("add", p(1, 5)),
  291. p(1, 8), p(1, 16), NoPos,
  292. intLit(1, p(1, 9)),
  293. intLit(2, p(1, 12)),
  294. intLit(3, p(1, 15)))),
  295. token.Assign,
  296. p(1, 3)))
  297. })
  298. expectParse(t, "a, b = add(1, 2, 3)", func(p pfn) []Stmt {
  299. return stmts(
  300. assignStmt(
  301. exprs(
  302. ident("a", p(1, 1)),
  303. ident("b", p(1, 4))),
  304. exprs(
  305. callExpr(
  306. ident("add", p(1, 8)),
  307. p(1, 11), p(1, 19), NoPos,
  308. intLit(1, p(1, 12)),
  309. intLit(2, p(1, 15)),
  310. intLit(3, p(1, 18)))),
  311. token.Assign,
  312. p(1, 6)))
  313. })
  314. expectParse(t, "add(a + 1, 2 * 1, (b + c))", func(p pfn) []Stmt {
  315. return stmts(
  316. exprStmt(
  317. callExpr(
  318. ident("add", p(1, 1)),
  319. p(1, 4), p(1, 26), NoPos,
  320. binaryExpr(
  321. ident("a", p(1, 5)),
  322. intLit(1, p(1, 9)),
  323. token.Add,
  324. p(1, 7)),
  325. binaryExpr(
  326. intLit(2, p(1, 12)),
  327. intLit(1, p(1, 16)),
  328. token.Mul,
  329. p(1, 14)),
  330. parenExpr(
  331. binaryExpr(
  332. ident("b", p(1, 20)),
  333. ident("c", p(1, 24)),
  334. token.Add,
  335. p(1, 22)),
  336. p(1, 19), p(1, 25)))))
  337. })
  338. expectParseString(t, "a + add(b * c) + d", "((a + add((b * c))) + d)")
  339. expectParseString(t, "add(a, b, 1, 2 * 3, 4 + 5, add(6, 7 * 8))",
  340. "add(a, b, 1, (2 * 3), (4 + 5), add(6, (7 * 8)))")
  341. expectParseString(t, "f1(a) + f2(b) * f3(c)", "(f1(a) + (f2(b) * f3(c)))")
  342. expectParseString(t, "(f1(a) + f2(b)) * f3(c)",
  343. "(((f1(a) + f2(b))) * f3(c))")
  344. expectParse(t, "func(a, b) { a + b }(1, 2)", func(p pfn) []Stmt {
  345. return stmts(
  346. exprStmt(
  347. callExpr(
  348. funcLit(
  349. funcType(
  350. identList(
  351. p(1, 5), p(1, 10),
  352. false,
  353. ident("a", p(1, 6)),
  354. ident("b", p(1, 9))),
  355. p(1, 1)),
  356. blockStmt(
  357. p(1, 12), p(1, 20),
  358. exprStmt(
  359. binaryExpr(
  360. ident("a", p(1, 14)),
  361. ident("b", p(1, 18)),
  362. token.Add,
  363. p(1, 16))))),
  364. p(1, 21), p(1, 26), NoPos,
  365. intLit(1, p(1, 22)),
  366. intLit(2, p(1, 25)))))
  367. })
  368. expectParse(t, `a.b()`, func(p pfn) []Stmt {
  369. return stmts(
  370. exprStmt(
  371. callExpr(
  372. selectorExpr(
  373. ident("a", p(1, 1)),
  374. stringLit("b", p(1, 3))),
  375. p(1, 4), p(1, 5), NoPos)))
  376. })
  377. expectParse(t, `a.b.c()`, func(p pfn) []Stmt {
  378. return stmts(
  379. exprStmt(
  380. callExpr(
  381. selectorExpr(
  382. selectorExpr(
  383. ident("a", p(1, 1)),
  384. stringLit("b", p(1, 3))),
  385. stringLit("c", p(1, 5))),
  386. p(1, 6), p(1, 7), NoPos)))
  387. })
  388. expectParse(t, `a["b"].c()`, func(p pfn) []Stmt {
  389. return stmts(
  390. exprStmt(
  391. callExpr(
  392. selectorExpr(
  393. indexExpr(
  394. ident("a", p(1, 1)),
  395. stringLit("b", p(1, 3)),
  396. p(1, 2), p(1, 6)),
  397. stringLit("c", p(1, 8))),
  398. p(1, 9), p(1, 10), NoPos)))
  399. })
  400. expectParseError(t, `add(...a, 1)`)
  401. expectParseError(t, `add(a..., 1)`)
  402. expectParseError(t, `add(a..., b...)`)
  403. expectParseError(t, `add(1, a..., b...)`)
  404. expectParseError(t, `add(...)`)
  405. expectParseError(t, `add(1, ...)`)
  406. expectParseError(t, `add(1, ..., )`)
  407. expectParseError(t, `add(...a)`)
  408. }
  409. func TestParseChar(t *testing.T) {
  410. expectParse(t, `'A'`, func(p pfn) []Stmt {
  411. return stmts(
  412. exprStmt(
  413. charLit('A', 1)))
  414. })
  415. expectParse(t, `'九'`, func(p pfn) []Stmt {
  416. return stmts(
  417. exprStmt(
  418. charLit('九', 1)))
  419. })
  420. expectParseError(t, `''`)
  421. expectParseError(t, `'AB'`)
  422. expectParseError(t, `'A九'`)
  423. }
  424. func TestParseCondExpr(t *testing.T) {
  425. expectParse(t, "a ? b : c", func(p pfn) []Stmt {
  426. return stmts(
  427. exprStmt(
  428. condExpr(
  429. ident("a", p(1, 1)),
  430. ident("b", p(1, 5)),
  431. ident("c", p(1, 9)),
  432. p(1, 3),
  433. p(1, 7))))
  434. })
  435. expectParse(t, `a ?
  436. b :
  437. c`, func(p pfn) []Stmt {
  438. return stmts(
  439. exprStmt(
  440. condExpr(
  441. ident("a", p(1, 1)),
  442. ident("b", p(1, 5)),
  443. ident("c", p(1, 9)),
  444. p(1, 3),
  445. p(1, 7))))
  446. })
  447. expectParseString(t, `a ? b : c`, "(a ? b : c)")
  448. expectParseString(t, `a + b ? c - d : e * f`,
  449. "((a + b) ? (c - d) : (e * f))")
  450. expectParseString(t, `a == b ? c + (d / e) : f ? g : h + i`,
  451. "((a == b) ? (c + ((d / e))) : (f ? g : (h + i)))")
  452. expectParseString(t, `(a + b) ? (c - d) : (e * f)`,
  453. "(((a + b)) ? ((c - d)) : ((e * f)))")
  454. expectParseString(t, `a + (b ? c : d) - e`, "((a + ((b ? c : d))) - e)")
  455. expectParseString(t, `a ? b ? c : d : e`, "(a ? (b ? c : d) : e)")
  456. expectParseString(t, `a := b ? c : d`, "a := (b ? c : d)")
  457. expectParseString(t, `x := a ? b ? c : d : e`,
  458. "x := (a ? (b ? c : d) : e)")
  459. // ? : should be at the end of each line if it's multi-line
  460. expectParseError(t, `a
  461. ? b
  462. : c`)
  463. expectParseError(t, `a ? (b : e)`)
  464. expectParseError(t, `(a ? b) : e`)
  465. }
  466. func TestParseError(t *testing.T) {
  467. expectParse(t, `error(1234)`, func(p pfn) []Stmt {
  468. return stmts(
  469. exprStmt(
  470. errorExpr(p(1, 1), intLit(1234, p(1, 7)), p(1, 6), p(1, 11))))
  471. })
  472. expectParse(t, `err1 := error("some error")`, func(p pfn) []Stmt {
  473. return stmts(
  474. assignStmt(
  475. exprs(ident("err1", p(1, 1))),
  476. exprs(errorExpr(p(1, 9),
  477. stringLit("some error", p(1, 15)), p(1, 14), p(1, 27))),
  478. token.Define, p(1, 6)))
  479. })
  480. expectParse(t, `return error("some error")`, func(p pfn) []Stmt {
  481. return stmts(
  482. returnStmt(p(1, 1),
  483. errorExpr(p(1, 8),
  484. stringLit("some error", p(1, 14)), p(1, 13), p(1, 26))))
  485. })
  486. expectParse(t, `return error("some" + "error")`, func(p pfn) []Stmt {
  487. return stmts(
  488. returnStmt(p(1, 1),
  489. errorExpr(p(1, 8),
  490. binaryExpr(
  491. stringLit("some", p(1, 14)),
  492. stringLit("error", p(1, 23)),
  493. token.Add, p(1, 21)),
  494. p(1, 13), p(1, 30))))
  495. })
  496. expectParseError(t, `error()`) // must have a value
  497. }
  498. func TestParseForIn(t *testing.T) {
  499. expectParse(t, "for x in y {}", func(p pfn) []Stmt {
  500. return stmts(
  501. forInStmt(
  502. ident("_", p(1, 5)),
  503. ident("x", p(1, 5)),
  504. ident("y", p(1, 10)),
  505. blockStmt(p(1, 12), p(1, 13)),
  506. p(1, 1)))
  507. })
  508. expectParse(t, "for _ in y {}", func(p pfn) []Stmt {
  509. return stmts(
  510. forInStmt(
  511. ident("_", p(1, 5)),
  512. ident("_", p(1, 5)),
  513. ident("y", p(1, 10)),
  514. blockStmt(p(1, 12), p(1, 13)),
  515. p(1, 1)))
  516. })
  517. expectParse(t, "for x in [1, 2, 3] {}", func(p pfn) []Stmt {
  518. return stmts(
  519. forInStmt(
  520. ident("_", p(1, 5)),
  521. ident("x", p(1, 5)),
  522. arrayLit(
  523. p(1, 10), p(1, 18),
  524. intLit(1, p(1, 11)),
  525. intLit(2, p(1, 14)),
  526. intLit(3, p(1, 17))),
  527. blockStmt(p(1, 20), p(1, 21)),
  528. p(1, 1)))
  529. })
  530. expectParse(t, "for x, y in z {}", func(p pfn) []Stmt {
  531. return stmts(
  532. forInStmt(
  533. ident("x", p(1, 5)),
  534. ident("y", p(1, 8)),
  535. ident("z", p(1, 13)),
  536. blockStmt(p(1, 15), p(1, 16)),
  537. p(1, 1)))
  538. })
  539. expectParse(t, "for x, y in {k1: 1, k2: 2} {}", func(p pfn) []Stmt {
  540. return stmts(
  541. forInStmt(
  542. ident("x", p(1, 5)),
  543. ident("y", p(1, 8)),
  544. mapLit(
  545. p(1, 13), p(1, 26),
  546. mapElementLit(
  547. "k1", p(1, 14), p(1, 16), intLit(1, p(1, 18))),
  548. mapElementLit(
  549. "k2", p(1, 21), p(1, 23), intLit(2, p(1, 25)))),
  550. blockStmt(p(1, 28), p(1, 29)),
  551. p(1, 1)))
  552. })
  553. }
  554. func TestParseFor(t *testing.T) {
  555. expectParse(t, "for {}", func(p pfn) []Stmt {
  556. return stmts(
  557. forStmt(nil, nil, nil, blockStmt(p(1, 5), p(1, 6)), p(1, 1)))
  558. })
  559. expectParse(t, "for a == 5 {}", func(p pfn) []Stmt {
  560. return stmts(
  561. forStmt(
  562. nil,
  563. binaryExpr(
  564. ident("a", p(1, 5)),
  565. intLit(5, p(1, 10)),
  566. token.Equal,
  567. p(1, 7)),
  568. nil,
  569. blockStmt(p(1, 12), p(1, 13)),
  570. p(1, 1)))
  571. })
  572. expectParse(t, "for a := 0; a == 5; {}", func(p pfn) []Stmt {
  573. return stmts(
  574. forStmt(
  575. assignStmt(
  576. exprs(ident("a", p(1, 5))),
  577. exprs(intLit(0, p(1, 10))),
  578. token.Define, p(1, 7)),
  579. binaryExpr(
  580. ident("a", p(1, 13)),
  581. intLit(5, p(1, 18)),
  582. token.Equal,
  583. p(1, 15)),
  584. nil,
  585. blockStmt(p(1, 22), p(1, 23)),
  586. p(1, 1)))
  587. })
  588. expectParse(t, "for a := 0; a < 5; a++ {}", func(p pfn) []Stmt {
  589. return stmts(
  590. forStmt(
  591. assignStmt(
  592. exprs(ident("a", p(1, 5))),
  593. exprs(intLit(0, p(1, 10))),
  594. token.Define, p(1, 7)),
  595. binaryExpr(
  596. ident("a", p(1, 13)),
  597. intLit(5, p(1, 17)),
  598. token.Less,
  599. p(1, 15)),
  600. incDecStmt(
  601. ident("a", p(1, 20)),
  602. token.Inc, p(1, 21)),
  603. blockStmt(p(1, 24), p(1, 25)),
  604. p(1, 1)))
  605. })
  606. expectParse(t, "for ; a < 5; a++ {}", func(p pfn) []Stmt {
  607. return stmts(
  608. forStmt(
  609. nil,
  610. binaryExpr(
  611. ident("a", p(1, 7)),
  612. intLit(5, p(1, 11)),
  613. token.Less,
  614. p(1, 9)),
  615. incDecStmt(
  616. ident("a", p(1, 14)),
  617. token.Inc, p(1, 15)),
  618. blockStmt(p(1, 18), p(1, 19)),
  619. p(1, 1)))
  620. })
  621. expectParse(t, "for a := 0; ; a++ {}", func(p pfn) []Stmt {
  622. return stmts(
  623. forStmt(
  624. assignStmt(
  625. exprs(ident("a", p(1, 5))),
  626. exprs(intLit(0, p(1, 10))),
  627. token.Define, p(1, 7)),
  628. nil,
  629. incDecStmt(
  630. ident("a", p(1, 15)),
  631. token.Inc, p(1, 16)),
  632. blockStmt(p(1, 19), p(1, 20)),
  633. p(1, 1)))
  634. })
  635. expectParse(t, "for a == 5 && b != 4 {}", func(p pfn) []Stmt {
  636. return stmts(
  637. forStmt(
  638. nil,
  639. binaryExpr(
  640. binaryExpr(
  641. ident("a", p(1, 5)),
  642. intLit(5, p(1, 10)),
  643. token.Equal,
  644. p(1, 7)),
  645. binaryExpr(
  646. ident("b", p(1, 15)),
  647. intLit(4, p(1, 20)),
  648. token.NotEqual,
  649. p(1, 17)),
  650. token.LAnd,
  651. p(1, 12)),
  652. nil,
  653. blockStmt(p(1, 22), p(1, 23)),
  654. p(1, 1)))
  655. })
  656. }
  657. func TestParseFunction(t *testing.T) {
  658. expectParse(t, "a = func(b, c, d) { return d }", func(p pfn) []Stmt {
  659. return stmts(
  660. assignStmt(
  661. exprs(
  662. ident("a", p(1, 1))),
  663. exprs(
  664. funcLit(
  665. funcType(
  666. identList(p(1, 9), p(1, 17), false,
  667. ident("b", p(1, 10)),
  668. ident("c", p(1, 13)),
  669. ident("d", p(1, 16))),
  670. p(1, 5)),
  671. blockStmt(p(1, 19), p(1, 30),
  672. returnStmt(p(1, 21), ident("d", p(1, 28)))))),
  673. token.Assign,
  674. p(1, 3)))
  675. })
  676. }
  677. func TestParseVariadicFunction(t *testing.T) {
  678. expectParse(t, "a = func(...args) { return args }", func(p pfn) []Stmt {
  679. return stmts(
  680. assignStmt(
  681. exprs(
  682. ident("a", p(1, 1))),
  683. exprs(
  684. funcLit(
  685. funcType(
  686. identList(
  687. p(1, 9), p(1, 17),
  688. true,
  689. ident("args", p(1, 13)),
  690. ), p(1, 5)),
  691. blockStmt(p(1, 19), p(1, 33),
  692. returnStmt(p(1, 21),
  693. ident("args", p(1, 28)),
  694. ),
  695. ),
  696. ),
  697. ),
  698. token.Assign,
  699. p(1, 3)))
  700. })
  701. }
  702. func TestParseVariadicFunctionWithArgs(t *testing.T) {
  703. expectParse(t, "a = func(x, y, ...z) { return z }", func(p pfn) []Stmt {
  704. return stmts(
  705. assignStmt(
  706. exprs(
  707. ident("a", p(1, 1))),
  708. exprs(
  709. funcLit(
  710. funcType(
  711. identList(
  712. p(1, 9), p(1, 20),
  713. true,
  714. ident("x", p(1, 10)),
  715. ident("y", p(1, 13)),
  716. ident("z", p(1, 19)),
  717. ), p(1, 5)),
  718. blockStmt(p(1, 22), p(1, 33),
  719. returnStmt(p(1, 24),
  720. ident("z", p(1, 31)),
  721. ),
  722. ),
  723. ),
  724. ),
  725. token.Assign,
  726. p(1, 3)))
  727. })
  728. expectParseError(t, "a = func(x, y, ...z, invalid) { return z }")
  729. expectParseError(t, "a = func(...args, invalid) { return args }")
  730. }
  731. func TestParseIf(t *testing.T) {
  732. expectParse(t, "if a == 5 {}", func(p pfn) []Stmt {
  733. return stmts(
  734. ifStmt(
  735. nil,
  736. binaryExpr(
  737. ident("a", p(1, 4)),
  738. intLit(5, p(1, 9)),
  739. token.Equal,
  740. p(1, 6)),
  741. blockStmt(
  742. p(1, 11), p(1, 12)),
  743. nil,
  744. p(1, 1)))
  745. })
  746. expectParse(t, "if a == 5 && b != 3 {}", func(p pfn) []Stmt {
  747. return stmts(
  748. ifStmt(
  749. nil,
  750. binaryExpr(
  751. binaryExpr(
  752. ident("a", p(1, 4)),
  753. intLit(5, p(1, 9)),
  754. token.Equal,
  755. p(1, 6)),
  756. binaryExpr(
  757. ident("b", p(1, 14)),
  758. intLit(3, p(1, 19)),
  759. token.NotEqual,
  760. p(1, 16)),
  761. token.LAnd,
  762. p(1, 11)),
  763. blockStmt(
  764. p(1, 21), p(1, 22)),
  765. nil,
  766. p(1, 1)))
  767. })
  768. expectParse(t, "if a == 5 { a = 3; a = 1 }", func(p pfn) []Stmt {
  769. return stmts(
  770. ifStmt(
  771. nil,
  772. binaryExpr(
  773. ident("a", p(1, 4)),
  774. intLit(5, p(1, 9)),
  775. token.Equal,
  776. p(1, 6)),
  777. blockStmt(
  778. p(1, 11), p(1, 26),
  779. assignStmt(
  780. exprs(ident("a", p(1, 13))),
  781. exprs(intLit(3, p(1, 17))),
  782. token.Assign,
  783. p(1, 15)),
  784. assignStmt(
  785. exprs(ident("a", p(1, 20))),
  786. exprs(intLit(1, p(1, 24))),
  787. token.Assign,
  788. p(1, 22))),
  789. nil,
  790. p(1, 1)))
  791. })
  792. expectParse(t, "if a == 5 { a = 3; a = 1 } else { a = 2; a = 4 }",
  793. func(p pfn) []Stmt {
  794. return stmts(
  795. ifStmt(
  796. nil,
  797. binaryExpr(
  798. ident("a", p(1, 4)),
  799. intLit(5, p(1, 9)),
  800. token.Equal,
  801. p(1, 6)),
  802. blockStmt(
  803. p(1, 11), p(1, 26),
  804. assignStmt(
  805. exprs(ident("a", p(1, 13))),
  806. exprs(intLit(3, p(1, 17))),
  807. token.Assign,
  808. p(1, 15)),
  809. assignStmt(
  810. exprs(ident("a", p(1, 20))),
  811. exprs(intLit(1, p(1, 24))),
  812. token.Assign,
  813. p(1, 22))),
  814. blockStmt(
  815. p(1, 33), p(1, 48),
  816. assignStmt(
  817. exprs(ident("a", p(1, 35))),
  818. exprs(intLit(2, p(1, 39))),
  819. token.Assign,
  820. p(1, 37)),
  821. assignStmt(
  822. exprs(ident("a", p(1, 42))),
  823. exprs(intLit(4, p(1, 46))),
  824. token.Assign,
  825. p(1, 44))),
  826. p(1, 1)))
  827. })
  828. expectParse(t, `
  829. if a == 5 {
  830. b = 3
  831. c = 1
  832. } else if d == 3 {
  833. e = 8
  834. f = 3
  835. } else {
  836. g = 2
  837. h = 4
  838. }`, func(p pfn) []Stmt {
  839. return stmts(
  840. ifStmt(
  841. nil,
  842. binaryExpr(
  843. ident("a", p(2, 4)),
  844. intLit(5, p(2, 9)),
  845. token.Equal,
  846. p(2, 6)),
  847. blockStmt(
  848. p(2, 11), p(5, 1),
  849. assignStmt(
  850. exprs(ident("b", p(3, 2))),
  851. exprs(intLit(3, p(3, 6))),
  852. token.Assign,
  853. p(3, 4)),
  854. assignStmt(
  855. exprs(ident("c", p(4, 2))),
  856. exprs(intLit(1, p(4, 6))),
  857. token.Assign,
  858. p(4, 4))),
  859. ifStmt(
  860. nil,
  861. binaryExpr(
  862. ident("d", p(5, 11)),
  863. intLit(3, p(5, 16)),
  864. token.Equal,
  865. p(5, 13)),
  866. blockStmt(
  867. p(5, 18), p(8, 1),
  868. assignStmt(
  869. exprs(ident("e", p(6, 2))),
  870. exprs(intLit(8, p(6, 6))),
  871. token.Assign,
  872. p(6, 4)),
  873. assignStmt(
  874. exprs(ident("f", p(7, 2))),
  875. exprs(intLit(3, p(7, 6))),
  876. token.Assign,
  877. p(7, 4))),
  878. blockStmt(
  879. p(8, 8), p(11, 1),
  880. assignStmt(
  881. exprs(ident("g", p(9, 2))),
  882. exprs(intLit(2, p(9, 6))),
  883. token.Assign,
  884. p(9, 4)),
  885. assignStmt(
  886. exprs(ident("h", p(10, 2))),
  887. exprs(intLit(4, p(10, 6))),
  888. token.Assign,
  889. p(10, 4))),
  890. p(5, 8)),
  891. p(2, 1)))
  892. })
  893. expectParse(t, "if a := 3; a < b {}", func(p pfn) []Stmt {
  894. return stmts(
  895. ifStmt(
  896. assignStmt(
  897. exprs(ident("a", p(1, 4))),
  898. exprs(intLit(3, p(1, 9))),
  899. token.Define, p(1, 6)),
  900. binaryExpr(
  901. ident("a", p(1, 12)),
  902. ident("b", p(1, 16)),
  903. token.Less, p(1, 14)),
  904. blockStmt(
  905. p(1, 18), p(1, 19)),
  906. nil,
  907. p(1, 1)))
  908. })
  909. expectParse(t, "if a++; a < b {}", func(p pfn) []Stmt {
  910. return stmts(
  911. ifStmt(
  912. incDecStmt(ident("a", p(1, 4)), token.Inc, p(1, 5)),
  913. binaryExpr(
  914. ident("a", p(1, 9)),
  915. ident("b", p(1, 13)),
  916. token.Less, p(1, 11)),
  917. blockStmt(
  918. p(1, 15), p(1, 16)),
  919. nil,
  920. p(1, 1)))
  921. })
  922. expectParseError(t, `if {}`)
  923. expectParseError(t, `if a == b { } else a != b { }`)
  924. expectParseError(t, `if a == b { } else if { }`)
  925. expectParseError(t, `else { }`)
  926. expectParseError(t, `if ; {}`)
  927. expectParseError(t, `if a := 3; {}`)
  928. expectParseError(t, `if ; a < 3 {}`)
  929. }
  930. func TestParseImport(t *testing.T) {
  931. expectParse(t, `a := import("mod1")`, func(p pfn) []Stmt {
  932. return stmts(
  933. assignStmt(
  934. exprs(ident("a", p(1, 1))),
  935. exprs(importExpr("mod1", p(1, 6))),
  936. token.Define, p(1, 3)))
  937. })
  938. expectParse(t, `import("mod1").var1`, func(p pfn) []Stmt {
  939. return stmts(
  940. exprStmt(
  941. selectorExpr(
  942. importExpr("mod1", p(1, 1)),
  943. stringLit("var1", p(1, 16)))))
  944. })
  945. expectParse(t, `import("mod1").func1()`, func(p pfn) []Stmt {
  946. return stmts(
  947. exprStmt(
  948. callExpr(
  949. selectorExpr(
  950. importExpr("mod1", p(1, 1)),
  951. stringLit("func1", p(1, 16))),
  952. p(1, 21), p(1, 22), NoPos)))
  953. })
  954. expectParse(t, `for x, y in import("mod1") {}`, func(p pfn) []Stmt {
  955. return stmts(
  956. forInStmt(
  957. ident("x", p(1, 5)),
  958. ident("y", p(1, 8)),
  959. importExpr("mod1", p(1, 13)),
  960. blockStmt(p(1, 28), p(1, 29)),
  961. p(1, 1)))
  962. })
  963. }
  964. func TestParseIndex(t *testing.T) {
  965. expectParse(t, "[1, 2, 3][1]", func(p pfn) []Stmt {
  966. return stmts(
  967. exprStmt(
  968. indexExpr(
  969. arrayLit(p(1, 1), p(1, 9),
  970. intLit(1, p(1, 2)),
  971. intLit(2, p(1, 5)),
  972. intLit(3, p(1, 8))),
  973. intLit(1, p(1, 11)),
  974. p(1, 10), p(1, 12))))
  975. })
  976. expectParse(t, "[1, 2, 3][5 - a]", func(p pfn) []Stmt {
  977. return stmts(
  978. exprStmt(
  979. indexExpr(
  980. arrayLit(p(1, 1), p(1, 9),
  981. intLit(1, p(1, 2)),
  982. intLit(2, p(1, 5)),
  983. intLit(3, p(1, 8))),
  984. binaryExpr(
  985. intLit(5, p(1, 11)),
  986. ident("a", p(1, 15)),
  987. token.Sub,
  988. p(1, 13)),
  989. p(1, 10), p(1, 16))))
  990. })
  991. expectParse(t, "[1, 2, 3][5 : a]", func(p pfn) []Stmt {
  992. return stmts(
  993. exprStmt(
  994. sliceExpr(
  995. arrayLit(p(1, 1), p(1, 9),
  996. intLit(1, p(1, 2)),
  997. intLit(2, p(1, 5)),
  998. intLit(3, p(1, 8))),
  999. intLit(5, p(1, 11)),
  1000. ident("a", p(1, 15)),
  1001. p(1, 10), p(1, 16))))
  1002. })
  1003. expectParse(t, "[1, 2, 3][a + 3 : b - 8]", func(p pfn) []Stmt {
  1004. return stmts(
  1005. exprStmt(
  1006. sliceExpr(
  1007. arrayLit(p(1, 1), p(1, 9),
  1008. intLit(1, p(1, 2)),
  1009. intLit(2, p(1, 5)),
  1010. intLit(3, p(1, 8))),
  1011. binaryExpr(
  1012. ident("a", p(1, 11)),
  1013. intLit(3, p(1, 15)),
  1014. token.Add,
  1015. p(1, 13)),
  1016. binaryExpr(
  1017. ident("b", p(1, 19)),
  1018. intLit(8, p(1, 23)),
  1019. token.Sub,
  1020. p(1, 21)),
  1021. p(1, 10), p(1, 24))))
  1022. })
  1023. expectParse(t, `{a: 1, b: 2}["b"]`, func(p pfn) []Stmt {
  1024. return stmts(
  1025. exprStmt(
  1026. indexExpr(
  1027. mapLit(p(1, 1), p(1, 12),
  1028. mapElementLit(
  1029. "a", p(1, 2), p(1, 3), intLit(1, p(1, 5))),
  1030. mapElementLit(
  1031. "b", p(1, 8), p(1, 9), intLit(2, p(1, 11)))),
  1032. stringLit("b", p(1, 14)),
  1033. p(1, 13), p(1, 17))))
  1034. })
  1035. expectParse(t, `{a: 1, b: 2}[a + b]`, func(p pfn) []Stmt {
  1036. return stmts(
  1037. exprStmt(
  1038. indexExpr(
  1039. mapLit(p(1, 1), p(1, 12),
  1040. mapElementLit(
  1041. "a", p(1, 2), p(1, 3), intLit(1, p(1, 5))),
  1042. mapElementLit(
  1043. "b", p(1, 8), p(1, 9), intLit(2, p(1, 11)))),
  1044. binaryExpr(
  1045. ident("a", p(1, 14)),
  1046. ident("b", p(1, 18)),
  1047. token.Add,
  1048. p(1, 16)),
  1049. p(1, 13), p(1, 19))))
  1050. })
  1051. }
  1052. func TestParseLogical(t *testing.T) {
  1053. expectParse(t, "a && 5 || true", func(p pfn) []Stmt {
  1054. return stmts(
  1055. exprStmt(
  1056. binaryExpr(
  1057. binaryExpr(
  1058. ident("a", p(1, 1)),
  1059. intLit(5, p(1, 6)),
  1060. token.LAnd,
  1061. p(1, 3)),
  1062. boolLit(true, p(1, 11)),
  1063. token.LOr,
  1064. p(1, 8))))
  1065. })
  1066. expectParse(t, "a || 5 && true", func(p pfn) []Stmt {
  1067. return stmts(
  1068. exprStmt(
  1069. binaryExpr(
  1070. ident("a", p(1, 1)),
  1071. binaryExpr(
  1072. intLit(5, p(1, 6)),
  1073. boolLit(true, p(1, 11)),
  1074. token.LAnd,
  1075. p(1, 8)),
  1076. token.LOr,
  1077. p(1, 3))))
  1078. })
  1079. expectParse(t, "a && (5 || true)", func(p pfn) []Stmt {
  1080. return stmts(
  1081. exprStmt(
  1082. binaryExpr(
  1083. ident("a", p(1, 1)),
  1084. parenExpr(
  1085. binaryExpr(
  1086. intLit(5, p(1, 7)),
  1087. boolLit(true, p(1, 12)),
  1088. token.LOr,
  1089. p(1, 9)),
  1090. p(1, 6), p(1, 16)),
  1091. token.LAnd,
  1092. p(1, 3))))
  1093. })
  1094. }
  1095. func TestParseMap(t *testing.T) {
  1096. expectParse(t, "{ key1: 1, key2: \"2\", key3: true }", func(p pfn) []Stmt {
  1097. return stmts(
  1098. exprStmt(
  1099. mapLit(p(1, 1), p(1, 34),
  1100. mapElementLit(
  1101. "key1", p(1, 3), p(1, 7), intLit(1, p(1, 9))),
  1102. mapElementLit(
  1103. "key2", p(1, 12), p(1, 16), stringLit("2", p(1, 18))),
  1104. mapElementLit(
  1105. "key3", p(1, 23), p(1, 27), boolLit(true, p(1, 29))))))
  1106. })
  1107. expectParse(t, "{ \"key1\": 1 }", func(p pfn) []Stmt {
  1108. return stmts(
  1109. exprStmt(
  1110. mapLit(p(1, 1), p(1, 13),
  1111. mapElementLit(
  1112. "key1", p(1, 3), p(1, 9), intLit(1, p(1, 11))))))
  1113. })
  1114. expectParse(t, "a = { key1: 1, key2: \"2\", key3: true }",
  1115. func(p pfn) []Stmt {
  1116. return stmts(assignStmt(
  1117. exprs(ident("a", p(1, 1))),
  1118. exprs(mapLit(p(1, 5), p(1, 38),
  1119. mapElementLit(
  1120. "key1", p(1, 7), p(1, 11), intLit(1, p(1, 13))),
  1121. mapElementLit(
  1122. "key2", p(1, 16), p(1, 20), stringLit("2", p(1, 22))),
  1123. mapElementLit(
  1124. "key3", p(1, 27), p(1, 31), boolLit(true, p(1, 33))))),
  1125. token.Assign,
  1126. p(1, 3)))
  1127. })
  1128. expectParse(t, "a = { key1: 1, key2: \"2\", key3: { k1: `bar`, k2: 4 } }",
  1129. func(p pfn) []Stmt {
  1130. return stmts(assignStmt(
  1131. exprs(ident("a", p(1, 1))),
  1132. exprs(mapLit(p(1, 5), p(1, 54),
  1133. mapElementLit(
  1134. "key1", p(1, 7), p(1, 11), intLit(1, p(1, 13))),
  1135. mapElementLit(
  1136. "key2", p(1, 16), p(1, 20), stringLit("2", p(1, 22))),
  1137. mapElementLit(
  1138. "key3", p(1, 27), p(1, 31),
  1139. mapLit(p(1, 33), p(1, 52),
  1140. mapElementLit(
  1141. "k1", p(1, 35),
  1142. p(1, 37), stringLit("bar", p(1, 39))),
  1143. mapElementLit(
  1144. "k2", p(1, 46),
  1145. p(1, 48), intLit(4, p(1, 50))))))),
  1146. token.Assign,
  1147. p(1, 3)))
  1148. })
  1149. expectParse(t, `
  1150. {
  1151. key1: 1,
  1152. key2: "2",
  1153. key3: true
  1154. }`, func(p pfn) []Stmt {
  1155. return stmts(exprStmt(
  1156. mapLit(p(2, 1), p(6, 1),
  1157. mapElementLit(
  1158. "key1", p(3, 2), p(3, 6), intLit(1, p(3, 8))),
  1159. mapElementLit(
  1160. "key2", p(4, 2), p(4, 6), stringLit("2", p(4, 8))),
  1161. mapElementLit(
  1162. "key3", p(5, 2), p(5, 6), boolLit(true, p(5, 8))))))
  1163. })
  1164. expectParseError(t, `
  1165. {
  1166. key1: 1,
  1167. key2: "2",
  1168. key3: true,
  1169. }`) // unlike Go, trailing comma for the last element is illegal
  1170. expectParseError(t, `{ key1: 1, }`)
  1171. expectParseError(t, `{
  1172. key1: 1,
  1173. key2: 2,
  1174. }`)
  1175. }
  1176. func TestParsePrecedence(t *testing.T) {
  1177. expectParseString(t, `a + b + c`, `((a + b) + c)`)
  1178. expectParseString(t, `a + b * c`, `(a + (b * c))`)
  1179. expectParseString(t, `x = 2 * 1 + 3 / 4`, `x = ((2 * 1) + (3 / 4))`)
  1180. }
  1181. func TestParseSelector(t *testing.T) {
  1182. expectParse(t, "a.b", func(p pfn) []Stmt {
  1183. return stmts(
  1184. exprStmt(
  1185. selectorExpr(
  1186. ident("a", p(1, 1)),
  1187. stringLit("b", p(1, 3)))))
  1188. })
  1189. expectParse(t, "a.b.c", func(p pfn) []Stmt {
  1190. return stmts(
  1191. exprStmt(
  1192. selectorExpr(
  1193. selectorExpr(
  1194. ident("a", p(1, 1)),
  1195. stringLit("b", p(1, 3))),
  1196. stringLit("c", p(1, 5)))))
  1197. })
  1198. expectParse(t, "{k1:1}.k1", func(p pfn) []Stmt {
  1199. return stmts(
  1200. exprStmt(
  1201. selectorExpr(
  1202. mapLit(
  1203. p(1, 1), p(1, 6),
  1204. mapElementLit(
  1205. "k1", p(1, 2), p(1, 4), intLit(1, p(1, 5)))),
  1206. stringLit("k1", p(1, 8)))))
  1207. })
  1208. expectParse(t, "{k1:{v1:1}}.k1.v1", func(p pfn) []Stmt {
  1209. return stmts(
  1210. exprStmt(
  1211. selectorExpr(
  1212. selectorExpr(
  1213. mapLit(
  1214. p(1, 1), p(1, 11),
  1215. mapElementLit("k1", p(1, 2), p(1, 4),
  1216. mapLit(p(1, 5), p(1, 10),
  1217. mapElementLit(
  1218. "v1", p(1, 6),
  1219. p(1, 8), intLit(1, p(1, 9)))))),
  1220. stringLit("k1", p(1, 13))),
  1221. stringLit("v1", p(1, 16)))))
  1222. })
  1223. expectParse(t, "a.b = 4", func(p pfn) []Stmt {
  1224. return stmts(
  1225. assignStmt(
  1226. exprs(
  1227. selectorExpr(
  1228. ident("a", p(1, 1)),
  1229. stringLit("b", p(1, 3)))),
  1230. exprs(intLit(4, p(1, 7))),
  1231. token.Assign, p(1, 5)))
  1232. })
  1233. expectParse(t, "a.b.c = 4", func(p pfn) []Stmt {
  1234. return stmts(
  1235. assignStmt(
  1236. exprs(
  1237. selectorExpr(
  1238. selectorExpr(
  1239. ident("a", p(1, 1)),
  1240. stringLit("b", p(1, 3))),
  1241. stringLit("c", p(1, 5)))),
  1242. exprs(intLit(4, p(1, 9))),
  1243. token.Assign, p(1, 7)))
  1244. })
  1245. expectParse(t, "a.b.c = 4 + 5", func(p pfn) []Stmt {
  1246. return stmts(
  1247. assignStmt(
  1248. exprs(
  1249. selectorExpr(
  1250. selectorExpr(
  1251. ident("a", p(1, 1)),
  1252. stringLit("b", p(1, 3))),
  1253. stringLit("c", p(1, 5)))),
  1254. exprs(
  1255. binaryExpr(
  1256. intLit(4, p(1, 9)),
  1257. intLit(5, p(1, 13)),
  1258. token.Add,
  1259. p(1, 11))),
  1260. token.Assign, p(1, 7)))
  1261. })
  1262. expectParse(t, "a[0].c = 4", func(p pfn) []Stmt {
  1263. return stmts(
  1264. assignStmt(
  1265. exprs(
  1266. selectorExpr(
  1267. indexExpr(
  1268. ident("a", p(1, 1)),
  1269. intLit(0, p(1, 3)),
  1270. p(1, 2), p(1, 4)),
  1271. stringLit("c", p(1, 6)))),
  1272. exprs(intLit(4, p(1, 10))),
  1273. token.Assign, p(1, 8)))
  1274. })
  1275. expectParse(t, "a.b[0].c = 4", func(p pfn) []Stmt {
  1276. return stmts(
  1277. assignStmt(
  1278. exprs(
  1279. selectorExpr(
  1280. indexExpr(
  1281. selectorExpr(
  1282. ident("a", p(1, 1)),
  1283. stringLit("b", p(1, 3))),
  1284. intLit(0, p(1, 5)),
  1285. p(1, 4), p(1, 6)),
  1286. stringLit("c", p(1, 8)))),
  1287. exprs(intLit(4, p(1, 12))),
  1288. token.Assign, p(1, 10)))
  1289. })
  1290. expectParse(t, "a.b[0][2].c = 4", func(p pfn) []Stmt {
  1291. return stmts(
  1292. assignStmt(
  1293. exprs(
  1294. selectorExpr(
  1295. indexExpr(
  1296. indexExpr(
  1297. selectorExpr(
  1298. ident("a", p(1, 1)),
  1299. stringLit("b", p(1, 3))),
  1300. intLit(0, p(1, 5)),
  1301. p(1, 4), p(1, 6)),
  1302. intLit(2, p(1, 8)),
  1303. p(1, 7), p(1, 9)),
  1304. stringLit("c", p(1, 11)))),
  1305. exprs(intLit(4, p(1, 15))),
  1306. token.Assign, p(1, 13)))
  1307. })
  1308. expectParse(t, `a.b["key1"][2].c = 4`, func(p pfn) []Stmt {
  1309. return stmts(
  1310. assignStmt(
  1311. exprs(
  1312. selectorExpr(
  1313. indexExpr(
  1314. indexExpr(
  1315. selectorExpr(
  1316. ident("a", p(1, 1)),
  1317. stringLit("b", p(1, 3))),
  1318. stringLit("key1", p(1, 5)),
  1319. p(1, 4), p(1, 11)),
  1320. intLit(2, p(1, 13)),
  1321. p(1, 12), p(1, 14)),
  1322. stringLit("c", p(1, 16)))),
  1323. exprs(intLit(4, p(1, 20))),
  1324. token.Assign, p(1, 18)))
  1325. })
  1326. expectParse(t, "a[0].b[2].c = 4", func(p pfn) []Stmt {
  1327. return stmts(
  1328. assignStmt(
  1329. exprs(
  1330. selectorExpr(
  1331. indexExpr(
  1332. selectorExpr(
  1333. indexExpr(
  1334. ident("a", p(1, 1)),
  1335. intLit(0, p(1, 3)),
  1336. p(1, 2), p(1, 4)),
  1337. stringLit("b", p(1, 6))),
  1338. intLit(2, p(1, 8)),
  1339. p(1, 7), p(1, 9)),
  1340. stringLit("c", p(1, 11)))),
  1341. exprs(intLit(4, p(1, 15))),
  1342. token.Assign, p(1, 13)))
  1343. })
  1344. expectParseError(t, `a.(b.c)`)
  1345. }
  1346. func TestParseSemicolon(t *testing.T) {
  1347. expectParse(t, "1", func(p pfn) []Stmt {
  1348. return stmts(
  1349. exprStmt(intLit(1, p(1, 1))))
  1350. })
  1351. expectParse(t, "1;", func(p pfn) []Stmt {
  1352. return stmts(
  1353. exprStmt(intLit(1, p(1, 1))))
  1354. })
  1355. expectParse(t, "1;;", func(p pfn) []Stmt {
  1356. return stmts(
  1357. exprStmt(intLit(1, p(1, 1))),
  1358. emptyStmt(false, p(1, 3)))
  1359. })
  1360. expectParse(t, `1
  1361. `, func(p pfn) []Stmt {
  1362. return stmts(
  1363. exprStmt(intLit(1, p(1, 1))))
  1364. })
  1365. expectParse(t, `1
  1366. ;`, func(p pfn) []Stmt {
  1367. return stmts(
  1368. exprStmt(intLit(1, p(1, 1))),
  1369. emptyStmt(false, p(2, 1)))
  1370. })
  1371. expectParse(t, `1;
  1372. ;`, func(p pfn) []Stmt {
  1373. return stmts(
  1374. exprStmt(intLit(1, p(1, 1))),
  1375. emptyStmt(false, p(2, 1)))
  1376. })
  1377. }
  1378. func TestParseString(t *testing.T) {
  1379. expectParse(t, `a = "foo\nbar"`, func(p pfn) []Stmt {
  1380. return stmts(
  1381. assignStmt(
  1382. exprs(ident("a", p(1, 1))),
  1383. exprs(stringLit("foo\nbar", p(1, 5))),
  1384. token.Assign,
  1385. p(1, 3)))
  1386. })
  1387. expectParse(t, "a = `raw string`", func(p pfn) []Stmt {
  1388. return stmts(
  1389. assignStmt(
  1390. exprs(ident("a", p(1, 1))),
  1391. exprs(stringLit("raw string", p(1, 5))),
  1392. token.Assign,
  1393. p(1, 3)))
  1394. })
  1395. }
  1396. func TestParseInt(t *testing.T) {
  1397. testCases := []string{
  1398. // All valid digits
  1399. "1234567890",
  1400. "0b10",
  1401. "0o12345670",
  1402. "0x123456789abcdef0",
  1403. "0x123456789ABCDEF0",
  1404. // Alternative base prefixes
  1405. "010",
  1406. "0B10",
  1407. "0O10",
  1408. "0X10",
  1409. // Invalid digits
  1410. "0b2",
  1411. "08",
  1412. "0o8",
  1413. "1a",
  1414. "0xg",
  1415. // Range errors
  1416. "9223372036854775807",
  1417. "9223372036854775808", // invalid: range error
  1418. // Examples from specification (https://go.dev/ref/spec#Integer_literals)
  1419. "42",
  1420. "4_2",
  1421. "0600",
  1422. "0_600",
  1423. "0o600",
  1424. "0O600", // second character is capital letter 'O'
  1425. "0xBadFace",
  1426. "0xBad_Face",
  1427. "0x_67_7a_2f_cc_40_c6",
  1428. "170141183460469231731687303715884105727",
  1429. "170_141183_460469_231731_687303_715884_105727",
  1430. "42_", // invalid: _ must separate successive digits
  1431. "4__2", // invalid: only one _ at a time
  1432. "0_xBadFace", // invalid: _ must separate successive digits
  1433. }
  1434. for _, num := range testCases {
  1435. t.Run(num, func(t *testing.T) {
  1436. expected, err := strconv.ParseInt(num, 0, 64)
  1437. if err == nil {
  1438. expectParse(t, num, func(p pfn) []Stmt {
  1439. return stmts(exprStmt(intLit(expected, p(1, 1))))
  1440. })
  1441. } else {
  1442. expectParseError(t, num)
  1443. }
  1444. })
  1445. }
  1446. }
  1447. func TestParseFloat(t *testing.T) {
  1448. testCases := []string{
  1449. // Different placements of decimal point
  1450. ".0",
  1451. "0.",
  1452. "0.0",
  1453. "00.0",
  1454. "00.00",
  1455. "0.0.0",
  1456. "0..0",
  1457. // Ignoring leading zeros
  1458. "010.0",
  1459. "00010.0",
  1460. "08.0",
  1461. "0a.0", // ivalid: hex character
  1462. // Exponents
  1463. "1e1",
  1464. "1E1",
  1465. "1e1.1",
  1466. "1e+1",
  1467. "1e-1",
  1468. "1e+-1",
  1469. "0x1p1",
  1470. "0x10p1",
  1471. // Examples from language specifcation (https://go.dev/ref/spec#Floating-point_literals)
  1472. "0.",
  1473. "72.40",
  1474. "072.40", // == 72.40
  1475. "2.71828",
  1476. "1.e+0",
  1477. "6.67428e-11",
  1478. "1E6",
  1479. ".25",
  1480. ".12345E+5",
  1481. "1_5.", // == 15.0
  1482. "0.15e+0_2", // == 15.0
  1483. "0x1p-2", // == 0.25
  1484. "0x2.p10", // == 2048.0
  1485. "0x1.Fp+0", // == 1.9375
  1486. "0X.8p-0", // == 0.5
  1487. "0X_1FFFP-16", // == 0.1249847412109375
  1488. "0x.p1", // invalid: mantissa has no digits
  1489. "1p-2", // invalid: p exponent requires hexadecimal mantissa
  1490. "0x1.5e-2", // invalid: hexadecimal mantissa requires p exponent
  1491. "1_.5", // invalid: _ must separate successive digits
  1492. "1._5", // invalid: _ must separate successive digits
  1493. "1.5_e1", // invalid: _ must separate successive digits
  1494. "1.5e_1", // invalid: _ must separate successive digits
  1495. "1.5e1_", // invalid: _ must separate successive digits
  1496. }
  1497. for _, num := range testCases {
  1498. t.Run(num, func(t *testing.T) {
  1499. expected, err := strconv.ParseFloat(num, 64)
  1500. if err == nil {
  1501. expectParse(t, num, func(p pfn) []Stmt {
  1502. return stmts(exprStmt(floatLit(expected, p(1, 1))))
  1503. })
  1504. } else {
  1505. expectParseError(t, num)
  1506. }
  1507. })
  1508. }
  1509. }
  1510. func TestParseNumberExpressions(t *testing.T) {
  1511. expectParse(t, `0x15e+2`, func(p pfn) []Stmt {
  1512. return stmts(
  1513. exprStmt(
  1514. binaryExpr(
  1515. intLit(0x15e, p(1, 1)),
  1516. intLit(2, p(1, 7)),
  1517. token.Add,
  1518. p(1, 6))))
  1519. })
  1520. expectParse(t, `0-_42`, func(p pfn) []Stmt {
  1521. return stmts(
  1522. exprStmt(
  1523. binaryExpr(
  1524. intLit(0, p(1, 1)),
  1525. ident("_42", p(1, 3)),
  1526. token.Sub,
  1527. p(1, 2))))
  1528. })
  1529. }
  1530. type pfn func(int, int) Pos // position conversion function
  1531. type expectedFn func(pos pfn) []Stmt // callback function to return expected results
  1532. type parseTracer struct {
  1533. out []string
  1534. }
  1535. func (o *parseTracer) Write(p []byte) (n int, err error) {
  1536. o.out = append(o.out, string(p))
  1537. return len(p), nil
  1538. }
  1539. //type slowPrinter struct {
  1540. //}
  1541. //
  1542. //func (o *slowPrinter) Write(p []byte) (n int, err error) {
  1543. // fmt.Print(string(p))
  1544. // time.Sleep(25 * time.Millisecond)
  1545. // return len(p), nil
  1546. //}
  1547. func expectParse(t *testing.T, input string, fn expectedFn) {
  1548. testFileSet := NewFileSet()
  1549. testFile := testFileSet.AddFile("test", -1, len(input))
  1550. var ok bool
  1551. defer func() {
  1552. if !ok {
  1553. // print trace
  1554. tr := &parseTracer{}
  1555. p := NewParser(testFile, []byte(input), tr)
  1556. actual, _ := p.ParseFile()
  1557. if actual != nil {
  1558. t.Logf("Parsed:\n%s", actual.String())
  1559. }
  1560. t.Logf("Trace:\n%s", strings.Join(tr.out, ""))
  1561. }
  1562. }()
  1563. p := NewParser(testFile, []byte(input), nil)
  1564. actual, err := p.ParseFile()
  1565. require.NoError(t, err)
  1566. expected := fn(func(line, column int) Pos {
  1567. return Pos(int(testFile.LineStart(line)) + (column - 1))
  1568. })
  1569. require.Equal(t, len(expected), len(actual.Stmts))
  1570. for i := 0; i < len(expected); i++ {
  1571. equalStmt(t, expected[i], actual.Stmts[i])
  1572. }
  1573. ok = true
  1574. }
  1575. func expectParseError(t *testing.T, input string) {
  1576. testFileSet := NewFileSet()
  1577. testFile := testFileSet.AddFile("test", -1, len(input))
  1578. var ok bool
  1579. defer func() {
  1580. if !ok {
  1581. // print trace
  1582. tr := &parseTracer{}
  1583. p := NewParser(testFile, []byte(input), tr)
  1584. _, _ = p.ParseFile()
  1585. t.Logf("Trace:\n%s", strings.Join(tr.out, ""))
  1586. }
  1587. }()
  1588. p := NewParser(testFile, []byte(input), nil)
  1589. _, err := p.ParseFile()
  1590. require.Error(t, err)
  1591. ok = true
  1592. }
  1593. func expectParseString(t *testing.T, input, expected string) {
  1594. var ok bool
  1595. defer func() {
  1596. if !ok {
  1597. // print trace
  1598. tr := &parseTracer{}
  1599. _, _ = parseSource("test", []byte(input), tr)
  1600. t.Logf("Trace:\n%s", strings.Join(tr.out, ""))
  1601. }
  1602. }()
  1603. actual, err := parseSource("test", []byte(input), nil)
  1604. require.NoError(t, err)
  1605. require.Equal(t, expected, actual.String())
  1606. ok = true
  1607. }
  1608. func stmts(s ...Stmt) []Stmt {
  1609. return s
  1610. }
  1611. func exprStmt(x Expr) *ExprStmt {
  1612. return &ExprStmt{Expr: x}
  1613. }
  1614. func assignStmt(
  1615. lhs, rhs []Expr,
  1616. token token.Token,
  1617. pos Pos,
  1618. ) *AssignStmt {
  1619. return &AssignStmt{LHS: lhs, RHS: rhs, Token: token, TokenPos: pos}
  1620. }
  1621. func emptyStmt(implicit bool, pos Pos) *EmptyStmt {
  1622. return &EmptyStmt{Implicit: implicit, Semicolon: pos}
  1623. }
  1624. func returnStmt(pos Pos, result Expr) *ReturnStmt {
  1625. return &ReturnStmt{Result: result, ReturnPos: pos}
  1626. }
  1627. func forStmt(
  1628. init Stmt,
  1629. cond Expr,
  1630. post Stmt,
  1631. body *BlockStmt,
  1632. pos Pos,
  1633. ) *ForStmt {
  1634. return &ForStmt{
  1635. Cond: cond, Init: init, Post: post, Body: body, ForPos: pos,
  1636. }
  1637. }
  1638. func forInStmt(
  1639. key, value *Ident,
  1640. seq Expr,
  1641. body *BlockStmt,
  1642. pos Pos,
  1643. ) *ForInStmt {
  1644. return &ForInStmt{
  1645. Key: key, Value: value, Iterable: seq, Body: body, ForPos: pos,
  1646. }
  1647. }
  1648. func ifStmt(
  1649. init Stmt,
  1650. cond Expr,
  1651. body *BlockStmt,
  1652. elseStmt Stmt,
  1653. pos Pos,
  1654. ) *IfStmt {
  1655. return &IfStmt{
  1656. Init: init, Cond: cond, Body: body, Else: elseStmt, IfPos: pos,
  1657. }
  1658. }
  1659. func incDecStmt(
  1660. expr Expr,
  1661. tok token.Token,
  1662. pos Pos,
  1663. ) *IncDecStmt {
  1664. return &IncDecStmt{Expr: expr, Token: tok, TokenPos: pos}
  1665. }
  1666. func funcType(params *IdentList, pos Pos) *FuncType {
  1667. return &FuncType{Params: params, FuncPos: pos}
  1668. }
  1669. func blockStmt(lbrace, rbrace Pos, list ...Stmt) *BlockStmt {
  1670. return &BlockStmt{Stmts: list, LBrace: lbrace, RBrace: rbrace}
  1671. }
  1672. func ident(name string, pos Pos) *Ident {
  1673. return &Ident{Name: name, NamePos: pos}
  1674. }
  1675. func identList(
  1676. opening, closing Pos,
  1677. varArgs bool,
  1678. list ...*Ident,
  1679. ) *IdentList {
  1680. return &IdentList{
  1681. VarArgs: varArgs, List: list, LParen: opening, RParen: closing,
  1682. }
  1683. }
  1684. func binaryExpr(
  1685. x, y Expr,
  1686. op token.Token,
  1687. pos Pos,
  1688. ) *BinaryExpr {
  1689. return &BinaryExpr{LHS: x, RHS: y, Token: op, TokenPos: pos}
  1690. }
  1691. func condExpr(
  1692. cond, trueExpr, falseExpr Expr,
  1693. questionPos, colonPos Pos,
  1694. ) *CondExpr {
  1695. return &CondExpr{
  1696. Cond: cond, True: trueExpr, False: falseExpr,
  1697. QuestionPos: questionPos, ColonPos: colonPos,
  1698. }
  1699. }
  1700. func unaryExpr(x Expr, op token.Token, pos Pos) *UnaryExpr {
  1701. return &UnaryExpr{Expr: x, Token: op, TokenPos: pos}
  1702. }
  1703. func importExpr(moduleName string, pos Pos) *ImportExpr {
  1704. return &ImportExpr{
  1705. ModuleName: moduleName, Token: token.Import, TokenPos: pos,
  1706. }
  1707. }
  1708. func exprs(list ...Expr) []Expr {
  1709. return list
  1710. }
  1711. func intLit(value int64, pos Pos) *IntLit {
  1712. return &IntLit{Value: value, ValuePos: pos}
  1713. }
  1714. func floatLit(value float64, pos Pos) *FloatLit {
  1715. return &FloatLit{Value: value, ValuePos: pos}
  1716. }
  1717. func stringLit(value string, pos Pos) *StringLit {
  1718. return &StringLit{Value: value, ValuePos: pos}
  1719. }
  1720. func charLit(value rune, pos Pos) *CharLit {
  1721. return &CharLit{
  1722. Value: value, ValuePos: pos, Literal: fmt.Sprintf("'%c'", value),
  1723. }
  1724. }
  1725. func boolLit(value bool, pos Pos) *BoolLit {
  1726. return &BoolLit{Value: value, ValuePos: pos}
  1727. }
  1728. func arrayLit(lbracket, rbracket Pos, list ...Expr) *ArrayLit {
  1729. return &ArrayLit{LBrack: lbracket, RBrack: rbracket, Elements: list}
  1730. }
  1731. func mapElementLit(
  1732. key string,
  1733. keyPos Pos,
  1734. colonPos Pos,
  1735. value Expr,
  1736. ) *MapElementLit {
  1737. return &MapElementLit{
  1738. Key: key, KeyPos: keyPos, ColonPos: colonPos, Value: value,
  1739. }
  1740. }
  1741. func mapLit(
  1742. lbrace, rbrace Pos,
  1743. list ...*MapElementLit,
  1744. ) *MapLit {
  1745. return &MapLit{LBrace: lbrace, RBrace: rbrace, Elements: list}
  1746. }
  1747. func funcLit(funcType *FuncType, body *BlockStmt) *FuncLit {
  1748. return &FuncLit{Type: funcType, Body: body}
  1749. }
  1750. func parenExpr(x Expr, lparen, rparen Pos) *ParenExpr {
  1751. return &ParenExpr{Expr: x, LParen: lparen, RParen: rparen}
  1752. }
  1753. func callExpr(
  1754. f Expr,
  1755. lparen, rparen, ellipsis Pos,
  1756. args ...Expr,
  1757. ) *CallExpr {
  1758. return &CallExpr{Func: f, LParen: lparen, RParen: rparen,
  1759. Ellipsis: ellipsis, Args: args}
  1760. }
  1761. func indexExpr(
  1762. x, index Expr,
  1763. lbrack, rbrack Pos,
  1764. ) *IndexExpr {
  1765. return &IndexExpr{
  1766. Expr: x, Index: index, LBrack: lbrack, RBrack: rbrack,
  1767. }
  1768. }
  1769. func sliceExpr(
  1770. x, low, high Expr,
  1771. lbrack, rbrack Pos,
  1772. ) *SliceExpr {
  1773. return &SliceExpr{
  1774. Expr: x, Low: low, High: high, LBrack: lbrack, RBrack: rbrack,
  1775. }
  1776. }
  1777. func errorExpr(
  1778. pos Pos,
  1779. x Expr,
  1780. lparen, rparen Pos,
  1781. ) *ErrorExpr {
  1782. return &ErrorExpr{
  1783. Expr: x, ErrorPos: pos, LParen: lparen, RParen: rparen,
  1784. }
  1785. }
  1786. func selectorExpr(x, sel Expr) *SelectorExpr {
  1787. return &SelectorExpr{Expr: x, Sel: sel}
  1788. }
  1789. func equalStmt(t *testing.T, expected, actual Stmt) {
  1790. if expected == nil || reflect.ValueOf(expected).IsNil() {
  1791. require.Nil(t, actual, "expected nil, but got not nil")
  1792. return
  1793. }
  1794. require.NotNil(t, actual, "expected not nil, but got nil")
  1795. require.IsType(t, expected, actual)
  1796. switch expected := expected.(type) {
  1797. case *ExprStmt:
  1798. equalExpr(t, expected.Expr, actual.(*ExprStmt).Expr)
  1799. case *EmptyStmt:
  1800. require.Equal(t, expected.Implicit,
  1801. actual.(*EmptyStmt).Implicit)
  1802. require.Equal(t, expected.Semicolon,
  1803. actual.(*EmptyStmt).Semicolon)
  1804. case *BlockStmt:
  1805. require.Equal(t, expected.LBrace,
  1806. actual.(*BlockStmt).LBrace)
  1807. require.Equal(t, expected.RBrace,
  1808. actual.(*BlockStmt).RBrace)
  1809. equalStmts(t, expected.Stmts,
  1810. actual.(*BlockStmt).Stmts)
  1811. case *AssignStmt:
  1812. equalExprs(t, expected.LHS,
  1813. actual.(*AssignStmt).LHS)
  1814. equalExprs(t, expected.RHS,
  1815. actual.(*AssignStmt).RHS)
  1816. require.Equal(t, int(expected.Token),
  1817. int(actual.(*AssignStmt).Token))
  1818. require.Equal(t, int(expected.TokenPos),
  1819. int(actual.(*AssignStmt).TokenPos))
  1820. case *IfStmt:
  1821. equalStmt(t, expected.Init, actual.(*IfStmt).Init)
  1822. equalExpr(t, expected.Cond, actual.(*IfStmt).Cond)
  1823. equalStmt(t, expected.Body, actual.(*IfStmt).Body)
  1824. equalStmt(t, expected.Else, actual.(*IfStmt).Else)
  1825. require.Equal(t, expected.IfPos, actual.(*IfStmt).IfPos)
  1826. case *IncDecStmt:
  1827. equalExpr(t, expected.Expr,
  1828. actual.(*IncDecStmt).Expr)
  1829. require.Equal(t, expected.Token,
  1830. actual.(*IncDecStmt).Token)
  1831. require.Equal(t, expected.TokenPos,
  1832. actual.(*IncDecStmt).TokenPos)
  1833. case *ForStmt:
  1834. equalStmt(t, expected.Init, actual.(*ForStmt).Init)
  1835. equalExpr(t, expected.Cond, actual.(*ForStmt).Cond)
  1836. equalStmt(t, expected.Post, actual.(*ForStmt).Post)
  1837. equalStmt(t, expected.Body, actual.(*ForStmt).Body)
  1838. require.Equal(t, expected.ForPos, actual.(*ForStmt).ForPos)
  1839. case *ForInStmt:
  1840. equalExpr(t, expected.Key,
  1841. actual.(*ForInStmt).Key)
  1842. equalExpr(t, expected.Value,
  1843. actual.(*ForInStmt).Value)
  1844. equalExpr(t, expected.Iterable,
  1845. actual.(*ForInStmt).Iterable)
  1846. equalStmt(t, expected.Body,
  1847. actual.(*ForInStmt).Body)
  1848. require.Equal(t, expected.ForPos,
  1849. actual.(*ForInStmt).ForPos)
  1850. case *ReturnStmt:
  1851. equalExpr(t, expected.Result,
  1852. actual.(*ReturnStmt).Result)
  1853. require.Equal(t, expected.ReturnPos,
  1854. actual.(*ReturnStmt).ReturnPos)
  1855. case *BranchStmt:
  1856. equalExpr(t, expected.Label,
  1857. actual.(*BranchStmt).Label)
  1858. require.Equal(t, expected.Token,
  1859. actual.(*BranchStmt).Token)
  1860. require.Equal(t, expected.TokenPos,
  1861. actual.(*BranchStmt).TokenPos)
  1862. default:
  1863. panic(fmt.Errorf("unknown type: %T", expected))
  1864. }
  1865. }
  1866. func equalExpr(t *testing.T, expected, actual Expr) {
  1867. if expected == nil || reflect.ValueOf(expected).IsNil() {
  1868. require.Nil(t, actual, "expected nil, but got not nil")
  1869. return
  1870. }
  1871. require.NotNil(t, actual, "expected not nil, but got nil")
  1872. require.IsType(t, expected, actual)
  1873. switch expected := expected.(type) {
  1874. case *Ident:
  1875. require.Equal(t, expected.Name,
  1876. actual.(*Ident).Name)
  1877. require.Equal(t, int(expected.NamePos),
  1878. int(actual.(*Ident).NamePos))
  1879. case *IntLit:
  1880. require.Equal(t, expected.Value,
  1881. actual.(*IntLit).Value)
  1882. require.Equal(t, int(expected.ValuePos),
  1883. int(actual.(*IntLit).ValuePos))
  1884. case *FloatLit:
  1885. require.Equal(t, expected.Value,
  1886. actual.(*FloatLit).Value)
  1887. require.Equal(t, int(expected.ValuePos),
  1888. int(actual.(*FloatLit).ValuePos))
  1889. case *BoolLit:
  1890. require.Equal(t, expected.Value,
  1891. actual.(*BoolLit).Value)
  1892. require.Equal(t, int(expected.ValuePos),
  1893. int(actual.(*BoolLit).ValuePos))
  1894. case *CharLit:
  1895. require.Equal(t, expected.Value,
  1896. actual.(*CharLit).Value)
  1897. require.Equal(t, int(expected.ValuePos),
  1898. int(actual.(*CharLit).ValuePos))
  1899. case *StringLit:
  1900. require.Equal(t, expected.Value,
  1901. actual.(*StringLit).Value)
  1902. require.Equal(t, int(expected.ValuePos),
  1903. int(actual.(*StringLit).ValuePos))
  1904. case *ArrayLit:
  1905. require.Equal(t, expected.LBrack,
  1906. actual.(*ArrayLit).LBrack)
  1907. require.Equal(t, expected.RBrack,
  1908. actual.(*ArrayLit).RBrack)
  1909. equalExprs(t, expected.Elements,
  1910. actual.(*ArrayLit).Elements)
  1911. case *MapLit:
  1912. require.Equal(t, expected.LBrace,
  1913. actual.(*MapLit).LBrace)
  1914. require.Equal(t, expected.RBrace,
  1915. actual.(*MapLit).RBrace)
  1916. equalMapElements(t, expected.Elements,
  1917. actual.(*MapLit).Elements)
  1918. case *BinaryExpr:
  1919. equalExpr(t, expected.LHS,
  1920. actual.(*BinaryExpr).LHS)
  1921. equalExpr(t, expected.RHS,
  1922. actual.(*BinaryExpr).RHS)
  1923. require.Equal(t, expected.Token,
  1924. actual.(*BinaryExpr).Token)
  1925. require.Equal(t, expected.TokenPos,
  1926. actual.(*BinaryExpr).TokenPos)
  1927. case *UnaryExpr:
  1928. equalExpr(t, expected.Expr,
  1929. actual.(*UnaryExpr).Expr)
  1930. require.Equal(t, expected.Token,
  1931. actual.(*UnaryExpr).Token)
  1932. require.Equal(t, expected.TokenPos,
  1933. actual.(*UnaryExpr).TokenPos)
  1934. case *FuncLit:
  1935. equalFuncType(t, expected.Type,
  1936. actual.(*FuncLit).Type)
  1937. equalStmt(t, expected.Body,
  1938. actual.(*FuncLit).Body)
  1939. case *CallExpr:
  1940. equalExpr(t, expected.Func,
  1941. actual.(*CallExpr).Func)
  1942. require.Equal(t, expected.LParen,
  1943. actual.(*CallExpr).LParen)
  1944. require.Equal(t, expected.RParen,
  1945. actual.(*CallExpr).RParen)
  1946. equalExprs(t, expected.Args,
  1947. actual.(*CallExpr).Args)
  1948. case *ParenExpr:
  1949. equalExpr(t, expected.Expr,
  1950. actual.(*ParenExpr).Expr)
  1951. require.Equal(t, expected.LParen,
  1952. actual.(*ParenExpr).LParen)
  1953. require.Equal(t, expected.RParen,
  1954. actual.(*ParenExpr).RParen)
  1955. case *IndexExpr:
  1956. equalExpr(t, expected.Expr,
  1957. actual.(*IndexExpr).Expr)
  1958. equalExpr(t, expected.Index,
  1959. actual.(*IndexExpr).Index)
  1960. require.Equal(t, expected.LBrack,
  1961. actual.(*IndexExpr).LBrack)
  1962. require.Equal(t, expected.RBrack,
  1963. actual.(*IndexExpr).RBrack)
  1964. case *SliceExpr:
  1965. equalExpr(t, expected.Expr,
  1966. actual.(*SliceExpr).Expr)
  1967. equalExpr(t, expected.Low,
  1968. actual.(*SliceExpr).Low)
  1969. equalExpr(t, expected.High,
  1970. actual.(*SliceExpr).High)
  1971. require.Equal(t, expected.LBrack,
  1972. actual.(*SliceExpr).LBrack)
  1973. require.Equal(t, expected.RBrack,
  1974. actual.(*SliceExpr).RBrack)
  1975. case *SelectorExpr:
  1976. equalExpr(t, expected.Expr,
  1977. actual.(*SelectorExpr).Expr)
  1978. equalExpr(t, expected.Sel,
  1979. actual.(*SelectorExpr).Sel)
  1980. case *ImportExpr:
  1981. require.Equal(t, expected.ModuleName,
  1982. actual.(*ImportExpr).ModuleName)
  1983. require.Equal(t, int(expected.TokenPos),
  1984. int(actual.(*ImportExpr).TokenPos))
  1985. require.Equal(t, expected.Token,
  1986. actual.(*ImportExpr).Token)
  1987. case *ErrorExpr:
  1988. equalExpr(t, expected.Expr,
  1989. actual.(*ErrorExpr).Expr)
  1990. require.Equal(t, int(expected.ErrorPos),
  1991. int(actual.(*ErrorExpr).ErrorPos))
  1992. require.Equal(t, int(expected.LParen),
  1993. int(actual.(*ErrorExpr).LParen))
  1994. require.Equal(t, int(expected.RParen),
  1995. int(actual.(*ErrorExpr).RParen))
  1996. case *CondExpr:
  1997. equalExpr(t, expected.Cond,
  1998. actual.(*CondExpr).Cond)
  1999. equalExpr(t, expected.True,
  2000. actual.(*CondExpr).True)
  2001. equalExpr(t, expected.False,
  2002. actual.(*CondExpr).False)
  2003. require.Equal(t, expected.QuestionPos,
  2004. actual.(*CondExpr).QuestionPos)
  2005. require.Equal(t, expected.ColonPos,
  2006. actual.(*CondExpr).ColonPos)
  2007. default:
  2008. panic(fmt.Errorf("unknown type: %T", expected))
  2009. }
  2010. }
  2011. func equalFuncType(t *testing.T, expected, actual *FuncType) {
  2012. require.Equal(t, expected.Params.LParen, actual.Params.LParen)
  2013. require.Equal(t, expected.Params.RParen, actual.Params.RParen)
  2014. equalIdents(t, expected.Params.List, actual.Params.List)
  2015. }
  2016. func equalIdents(t *testing.T, expected, actual []*Ident) {
  2017. require.Equal(t, len(expected), len(actual))
  2018. for i := 0; i < len(expected); i++ {
  2019. equalExpr(t, expected[i], actual[i])
  2020. }
  2021. }
  2022. func equalExprs(t *testing.T, expected, actual []Expr) {
  2023. require.Equal(t, len(expected), len(actual))
  2024. for i := 0; i < len(expected); i++ {
  2025. equalExpr(t, expected[i], actual[i])
  2026. }
  2027. }
  2028. func equalStmts(t *testing.T, expected, actual []Stmt) {
  2029. require.Equal(t, len(expected), len(actual))
  2030. for i := 0; i < len(expected); i++ {
  2031. equalStmt(t, expected[i], actual[i])
  2032. }
  2033. }
  2034. func equalMapElements(
  2035. t *testing.T,
  2036. expected, actual []*MapElementLit,
  2037. ) {
  2038. require.Equal(t, len(expected), len(actual))
  2039. for i := 0; i < len(expected); i++ {
  2040. require.Equal(t, expected[i].Key, actual[i].Key)
  2041. require.Equal(t, expected[i].KeyPos, actual[i].KeyPos)
  2042. require.Equal(t, expected[i].ColonPos, actual[i].ColonPos)
  2043. equalExpr(t, expected[i].Value, actual[i].Value)
  2044. }
  2045. }
  2046. func parseSource(
  2047. filename string,
  2048. src []byte,
  2049. trace io.Writer,
  2050. ) (res *File, err error) {
  2051. fileSet := NewFileSet()
  2052. file := fileSet.AddFile(filename, -1, len(src))
  2053. p := NewParser(file, src, trace)
  2054. return p.ParseFile()
  2055. }