parser_test.go 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253
  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 TestMismatchBrace(t *testing.T) {
  1511. expectParseError(t, `
  1512. fmt := import("fmt")
  1513. out := 0
  1514. if 3 == 1 {
  1515. out = 1
  1516. }
  1517. } else {
  1518. out = 2
  1519. }
  1520. fmt.println(out)
  1521. `)
  1522. }
  1523. func TestParseNumberExpressions(t *testing.T) {
  1524. expectParse(t, `0x15e+2`, func(p pfn) []Stmt {
  1525. return stmts(
  1526. exprStmt(
  1527. binaryExpr(
  1528. intLit(0x15e, p(1, 1)),
  1529. intLit(2, p(1, 7)),
  1530. token.Add,
  1531. p(1, 6))))
  1532. })
  1533. expectParse(t, `0-_42`, func(p pfn) []Stmt {
  1534. return stmts(
  1535. exprStmt(
  1536. binaryExpr(
  1537. intLit(0, p(1, 1)),
  1538. ident("_42", p(1, 3)),
  1539. token.Sub,
  1540. p(1, 2))))
  1541. })
  1542. }
  1543. type pfn func(int, int) Pos // position conversion function
  1544. type expectedFn func(pos pfn) []Stmt // callback function to return expected results
  1545. type parseTracer struct {
  1546. out []string
  1547. }
  1548. func (o *parseTracer) Write(p []byte) (n int, err error) {
  1549. o.out = append(o.out, string(p))
  1550. return len(p), nil
  1551. }
  1552. //type slowPrinter struct {
  1553. //}
  1554. //
  1555. //func (o *slowPrinter) Write(p []byte) (n int, err error) {
  1556. // fmt.Print(string(p))
  1557. // time.Sleep(25 * time.Millisecond)
  1558. // return len(p), nil
  1559. //}
  1560. func expectParse(t *testing.T, input string, fn expectedFn) {
  1561. testFileSet := NewFileSet()
  1562. testFile := testFileSet.AddFile("test", -1, len(input))
  1563. var ok bool
  1564. defer func() {
  1565. if !ok {
  1566. // print trace
  1567. tr := &parseTracer{}
  1568. p := NewParser(testFile, []byte(input), tr)
  1569. actual, _ := p.ParseFile()
  1570. if actual != nil {
  1571. t.Logf("Parsed:\n%s", actual.String())
  1572. }
  1573. t.Logf("Trace:\n%s", strings.Join(tr.out, ""))
  1574. }
  1575. }()
  1576. p := NewParser(testFile, []byte(input), nil)
  1577. actual, err := p.ParseFile()
  1578. require.NoError(t, err)
  1579. expected := fn(func(line, column int) Pos {
  1580. return Pos(int(testFile.LineStart(line)) + (column - 1))
  1581. })
  1582. require.Equal(t, len(expected), len(actual.Stmts))
  1583. for i := 0; i < len(expected); i++ {
  1584. equalStmt(t, expected[i], actual.Stmts[i])
  1585. }
  1586. ok = true
  1587. }
  1588. func expectParseError(t *testing.T, input string) {
  1589. testFileSet := NewFileSet()
  1590. testFile := testFileSet.AddFile("test", -1, len(input))
  1591. var ok bool
  1592. defer func() {
  1593. if !ok {
  1594. // print trace
  1595. tr := &parseTracer{}
  1596. p := NewParser(testFile, []byte(input), tr)
  1597. _, _ = p.ParseFile()
  1598. t.Logf("Trace:\n%s", strings.Join(tr.out, ""))
  1599. }
  1600. }()
  1601. p := NewParser(testFile, []byte(input), nil)
  1602. _, err := p.ParseFile()
  1603. require.Error(t, err)
  1604. ok = true
  1605. }
  1606. func expectParseString(t *testing.T, input, expected string) {
  1607. var ok bool
  1608. defer func() {
  1609. if !ok {
  1610. // print trace
  1611. tr := &parseTracer{}
  1612. _, _ = parseSource("test", []byte(input), tr)
  1613. t.Logf("Trace:\n%s", strings.Join(tr.out, ""))
  1614. }
  1615. }()
  1616. actual, err := parseSource("test", []byte(input), nil)
  1617. require.NoError(t, err)
  1618. require.Equal(t, expected, actual.String())
  1619. ok = true
  1620. }
  1621. func stmts(s ...Stmt) []Stmt {
  1622. return s
  1623. }
  1624. func exprStmt(x Expr) *ExprStmt {
  1625. return &ExprStmt{Expr: x}
  1626. }
  1627. func assignStmt(
  1628. lhs, rhs []Expr,
  1629. token token.Token,
  1630. pos Pos,
  1631. ) *AssignStmt {
  1632. return &AssignStmt{LHS: lhs, RHS: rhs, Token: token, TokenPos: pos}
  1633. }
  1634. func emptyStmt(implicit bool, pos Pos) *EmptyStmt {
  1635. return &EmptyStmt{Implicit: implicit, Semicolon: pos}
  1636. }
  1637. func returnStmt(pos Pos, result Expr) *ReturnStmt {
  1638. return &ReturnStmt{Result: result, ReturnPos: pos}
  1639. }
  1640. func forStmt(
  1641. init Stmt,
  1642. cond Expr,
  1643. post Stmt,
  1644. body *BlockStmt,
  1645. pos Pos,
  1646. ) *ForStmt {
  1647. return &ForStmt{
  1648. Cond: cond, Init: init, Post: post, Body: body, ForPos: pos,
  1649. }
  1650. }
  1651. func forInStmt(
  1652. key, value *Ident,
  1653. seq Expr,
  1654. body *BlockStmt,
  1655. pos Pos,
  1656. ) *ForInStmt {
  1657. return &ForInStmt{
  1658. Key: key, Value: value, Iterable: seq, Body: body, ForPos: pos,
  1659. }
  1660. }
  1661. func ifStmt(
  1662. init Stmt,
  1663. cond Expr,
  1664. body *BlockStmt,
  1665. elseStmt Stmt,
  1666. pos Pos,
  1667. ) *IfStmt {
  1668. return &IfStmt{
  1669. Init: init, Cond: cond, Body: body, Else: elseStmt, IfPos: pos,
  1670. }
  1671. }
  1672. func incDecStmt(
  1673. expr Expr,
  1674. tok token.Token,
  1675. pos Pos,
  1676. ) *IncDecStmt {
  1677. return &IncDecStmt{Expr: expr, Token: tok, TokenPos: pos}
  1678. }
  1679. func funcType(params *IdentList, pos Pos) *FuncType {
  1680. return &FuncType{Params: params, FuncPos: pos}
  1681. }
  1682. func blockStmt(lbrace, rbrace Pos, list ...Stmt) *BlockStmt {
  1683. return &BlockStmt{Stmts: list, LBrace: lbrace, RBrace: rbrace}
  1684. }
  1685. func ident(name string, pos Pos) *Ident {
  1686. return &Ident{Name: name, NamePos: pos}
  1687. }
  1688. func identList(
  1689. opening, closing Pos,
  1690. varArgs bool,
  1691. list ...*Ident,
  1692. ) *IdentList {
  1693. return &IdentList{
  1694. VarArgs: varArgs, List: list, LParen: opening, RParen: closing,
  1695. }
  1696. }
  1697. func binaryExpr(
  1698. x, y Expr,
  1699. op token.Token,
  1700. pos Pos,
  1701. ) *BinaryExpr {
  1702. return &BinaryExpr{LHS: x, RHS: y, Token: op, TokenPos: pos}
  1703. }
  1704. func condExpr(
  1705. cond, trueExpr, falseExpr Expr,
  1706. questionPos, colonPos Pos,
  1707. ) *CondExpr {
  1708. return &CondExpr{
  1709. Cond: cond, True: trueExpr, False: falseExpr,
  1710. QuestionPos: questionPos, ColonPos: colonPos,
  1711. }
  1712. }
  1713. func unaryExpr(x Expr, op token.Token, pos Pos) *UnaryExpr {
  1714. return &UnaryExpr{Expr: x, Token: op, TokenPos: pos}
  1715. }
  1716. func importExpr(moduleName string, pos Pos) *ImportExpr {
  1717. return &ImportExpr{
  1718. ModuleName: moduleName, Token: token.Import, TokenPos: pos,
  1719. }
  1720. }
  1721. func exprs(list ...Expr) []Expr {
  1722. return list
  1723. }
  1724. func intLit(value int64, pos Pos) *IntLit {
  1725. return &IntLit{Value: value, ValuePos: pos}
  1726. }
  1727. func floatLit(value float64, pos Pos) *FloatLit {
  1728. return &FloatLit{Value: value, ValuePos: pos}
  1729. }
  1730. func stringLit(value string, pos Pos) *StringLit {
  1731. return &StringLit{Value: value, ValuePos: pos}
  1732. }
  1733. func charLit(value rune, pos Pos) *CharLit {
  1734. return &CharLit{
  1735. Value: value, ValuePos: pos, Literal: fmt.Sprintf("'%c'", value),
  1736. }
  1737. }
  1738. func boolLit(value bool, pos Pos) *BoolLit {
  1739. return &BoolLit{Value: value, ValuePos: pos}
  1740. }
  1741. func arrayLit(lbracket, rbracket Pos, list ...Expr) *ArrayLit {
  1742. return &ArrayLit{LBrack: lbracket, RBrack: rbracket, Elements: list}
  1743. }
  1744. func mapElementLit(
  1745. key string,
  1746. keyPos Pos,
  1747. colonPos Pos,
  1748. value Expr,
  1749. ) *MapElementLit {
  1750. return &MapElementLit{
  1751. Key: key, KeyPos: keyPos, ColonPos: colonPos, Value: value,
  1752. }
  1753. }
  1754. func mapLit(
  1755. lbrace, rbrace Pos,
  1756. list ...*MapElementLit,
  1757. ) *MapLit {
  1758. return &MapLit{LBrace: lbrace, RBrace: rbrace, Elements: list}
  1759. }
  1760. func funcLit(funcType *FuncType, body *BlockStmt) *FuncLit {
  1761. return &FuncLit{Type: funcType, Body: body}
  1762. }
  1763. func parenExpr(x Expr, lparen, rparen Pos) *ParenExpr {
  1764. return &ParenExpr{Expr: x, LParen: lparen, RParen: rparen}
  1765. }
  1766. func callExpr(
  1767. f Expr,
  1768. lparen, rparen, ellipsis Pos,
  1769. args ...Expr,
  1770. ) *CallExpr {
  1771. return &CallExpr{Func: f, LParen: lparen, RParen: rparen,
  1772. Ellipsis: ellipsis, Args: args}
  1773. }
  1774. func indexExpr(
  1775. x, index Expr,
  1776. lbrack, rbrack Pos,
  1777. ) *IndexExpr {
  1778. return &IndexExpr{
  1779. Expr: x, Index: index, LBrack: lbrack, RBrack: rbrack,
  1780. }
  1781. }
  1782. func sliceExpr(
  1783. x, low, high Expr,
  1784. lbrack, rbrack Pos,
  1785. ) *SliceExpr {
  1786. return &SliceExpr{
  1787. Expr: x, Low: low, High: high, LBrack: lbrack, RBrack: rbrack,
  1788. }
  1789. }
  1790. func errorExpr(
  1791. pos Pos,
  1792. x Expr,
  1793. lparen, rparen Pos,
  1794. ) *ErrorExpr {
  1795. return &ErrorExpr{
  1796. Expr: x, ErrorPos: pos, LParen: lparen, RParen: rparen,
  1797. }
  1798. }
  1799. func selectorExpr(x, sel Expr) *SelectorExpr {
  1800. return &SelectorExpr{Expr: x, Sel: sel}
  1801. }
  1802. func equalStmt(t *testing.T, expected, actual Stmt) {
  1803. if expected == nil || reflect.ValueOf(expected).IsNil() {
  1804. require.Nil(t, actual, "expected nil, but got not nil")
  1805. return
  1806. }
  1807. require.NotNil(t, actual, "expected not nil, but got nil")
  1808. require.IsType(t, expected, actual)
  1809. switch expected := expected.(type) {
  1810. case *ExprStmt:
  1811. equalExpr(t, expected.Expr, actual.(*ExprStmt).Expr)
  1812. case *EmptyStmt:
  1813. require.Equal(t, expected.Implicit,
  1814. actual.(*EmptyStmt).Implicit)
  1815. require.Equal(t, expected.Semicolon,
  1816. actual.(*EmptyStmt).Semicolon)
  1817. case *BlockStmt:
  1818. require.Equal(t, expected.LBrace,
  1819. actual.(*BlockStmt).LBrace)
  1820. require.Equal(t, expected.RBrace,
  1821. actual.(*BlockStmt).RBrace)
  1822. equalStmts(t, expected.Stmts,
  1823. actual.(*BlockStmt).Stmts)
  1824. case *AssignStmt:
  1825. equalExprs(t, expected.LHS,
  1826. actual.(*AssignStmt).LHS)
  1827. equalExprs(t, expected.RHS,
  1828. actual.(*AssignStmt).RHS)
  1829. require.Equal(t, int(expected.Token),
  1830. int(actual.(*AssignStmt).Token))
  1831. require.Equal(t, int(expected.TokenPos),
  1832. int(actual.(*AssignStmt).TokenPos))
  1833. case *IfStmt:
  1834. equalStmt(t, expected.Init, actual.(*IfStmt).Init)
  1835. equalExpr(t, expected.Cond, actual.(*IfStmt).Cond)
  1836. equalStmt(t, expected.Body, actual.(*IfStmt).Body)
  1837. equalStmt(t, expected.Else, actual.(*IfStmt).Else)
  1838. require.Equal(t, expected.IfPos, actual.(*IfStmt).IfPos)
  1839. case *IncDecStmt:
  1840. equalExpr(t, expected.Expr,
  1841. actual.(*IncDecStmt).Expr)
  1842. require.Equal(t, expected.Token,
  1843. actual.(*IncDecStmt).Token)
  1844. require.Equal(t, expected.TokenPos,
  1845. actual.(*IncDecStmt).TokenPos)
  1846. case *ForStmt:
  1847. equalStmt(t, expected.Init, actual.(*ForStmt).Init)
  1848. equalExpr(t, expected.Cond, actual.(*ForStmt).Cond)
  1849. equalStmt(t, expected.Post, actual.(*ForStmt).Post)
  1850. equalStmt(t, expected.Body, actual.(*ForStmt).Body)
  1851. require.Equal(t, expected.ForPos, actual.(*ForStmt).ForPos)
  1852. case *ForInStmt:
  1853. equalExpr(t, expected.Key,
  1854. actual.(*ForInStmt).Key)
  1855. equalExpr(t, expected.Value,
  1856. actual.(*ForInStmt).Value)
  1857. equalExpr(t, expected.Iterable,
  1858. actual.(*ForInStmt).Iterable)
  1859. equalStmt(t, expected.Body,
  1860. actual.(*ForInStmt).Body)
  1861. require.Equal(t, expected.ForPos,
  1862. actual.(*ForInStmt).ForPos)
  1863. case *ReturnStmt:
  1864. equalExpr(t, expected.Result,
  1865. actual.(*ReturnStmt).Result)
  1866. require.Equal(t, expected.ReturnPos,
  1867. actual.(*ReturnStmt).ReturnPos)
  1868. case *BranchStmt:
  1869. equalExpr(t, expected.Label,
  1870. actual.(*BranchStmt).Label)
  1871. require.Equal(t, expected.Token,
  1872. actual.(*BranchStmt).Token)
  1873. require.Equal(t, expected.TokenPos,
  1874. actual.(*BranchStmt).TokenPos)
  1875. default:
  1876. panic(fmt.Errorf("unknown type: %T", expected))
  1877. }
  1878. }
  1879. func equalExpr(t *testing.T, expected, actual Expr) {
  1880. if expected == nil || reflect.ValueOf(expected).IsNil() {
  1881. require.Nil(t, actual, "expected nil, but got not nil")
  1882. return
  1883. }
  1884. require.NotNil(t, actual, "expected not nil, but got nil")
  1885. require.IsType(t, expected, actual)
  1886. switch expected := expected.(type) {
  1887. case *Ident:
  1888. require.Equal(t, expected.Name,
  1889. actual.(*Ident).Name)
  1890. require.Equal(t, int(expected.NamePos),
  1891. int(actual.(*Ident).NamePos))
  1892. case *IntLit:
  1893. require.Equal(t, expected.Value,
  1894. actual.(*IntLit).Value)
  1895. require.Equal(t, int(expected.ValuePos),
  1896. int(actual.(*IntLit).ValuePos))
  1897. case *FloatLit:
  1898. require.Equal(t, expected.Value,
  1899. actual.(*FloatLit).Value)
  1900. require.Equal(t, int(expected.ValuePos),
  1901. int(actual.(*FloatLit).ValuePos))
  1902. case *BoolLit:
  1903. require.Equal(t, expected.Value,
  1904. actual.(*BoolLit).Value)
  1905. require.Equal(t, int(expected.ValuePos),
  1906. int(actual.(*BoolLit).ValuePos))
  1907. case *CharLit:
  1908. require.Equal(t, expected.Value,
  1909. actual.(*CharLit).Value)
  1910. require.Equal(t, int(expected.ValuePos),
  1911. int(actual.(*CharLit).ValuePos))
  1912. case *StringLit:
  1913. require.Equal(t, expected.Value,
  1914. actual.(*StringLit).Value)
  1915. require.Equal(t, int(expected.ValuePos),
  1916. int(actual.(*StringLit).ValuePos))
  1917. case *ArrayLit:
  1918. require.Equal(t, expected.LBrack,
  1919. actual.(*ArrayLit).LBrack)
  1920. require.Equal(t, expected.RBrack,
  1921. actual.(*ArrayLit).RBrack)
  1922. equalExprs(t, expected.Elements,
  1923. actual.(*ArrayLit).Elements)
  1924. case *MapLit:
  1925. require.Equal(t, expected.LBrace,
  1926. actual.(*MapLit).LBrace)
  1927. require.Equal(t, expected.RBrace,
  1928. actual.(*MapLit).RBrace)
  1929. equalMapElements(t, expected.Elements,
  1930. actual.(*MapLit).Elements)
  1931. case *BinaryExpr:
  1932. equalExpr(t, expected.LHS,
  1933. actual.(*BinaryExpr).LHS)
  1934. equalExpr(t, expected.RHS,
  1935. actual.(*BinaryExpr).RHS)
  1936. require.Equal(t, expected.Token,
  1937. actual.(*BinaryExpr).Token)
  1938. require.Equal(t, expected.TokenPos,
  1939. actual.(*BinaryExpr).TokenPos)
  1940. case *UnaryExpr:
  1941. equalExpr(t, expected.Expr,
  1942. actual.(*UnaryExpr).Expr)
  1943. require.Equal(t, expected.Token,
  1944. actual.(*UnaryExpr).Token)
  1945. require.Equal(t, expected.TokenPos,
  1946. actual.(*UnaryExpr).TokenPos)
  1947. case *FuncLit:
  1948. equalFuncType(t, expected.Type,
  1949. actual.(*FuncLit).Type)
  1950. equalStmt(t, expected.Body,
  1951. actual.(*FuncLit).Body)
  1952. case *CallExpr:
  1953. equalExpr(t, expected.Func,
  1954. actual.(*CallExpr).Func)
  1955. require.Equal(t, expected.LParen,
  1956. actual.(*CallExpr).LParen)
  1957. require.Equal(t, expected.RParen,
  1958. actual.(*CallExpr).RParen)
  1959. equalExprs(t, expected.Args,
  1960. actual.(*CallExpr).Args)
  1961. case *ParenExpr:
  1962. equalExpr(t, expected.Expr,
  1963. actual.(*ParenExpr).Expr)
  1964. require.Equal(t, expected.LParen,
  1965. actual.(*ParenExpr).LParen)
  1966. require.Equal(t, expected.RParen,
  1967. actual.(*ParenExpr).RParen)
  1968. case *IndexExpr:
  1969. equalExpr(t, expected.Expr,
  1970. actual.(*IndexExpr).Expr)
  1971. equalExpr(t, expected.Index,
  1972. actual.(*IndexExpr).Index)
  1973. require.Equal(t, expected.LBrack,
  1974. actual.(*IndexExpr).LBrack)
  1975. require.Equal(t, expected.RBrack,
  1976. actual.(*IndexExpr).RBrack)
  1977. case *SliceExpr:
  1978. equalExpr(t, expected.Expr,
  1979. actual.(*SliceExpr).Expr)
  1980. equalExpr(t, expected.Low,
  1981. actual.(*SliceExpr).Low)
  1982. equalExpr(t, expected.High,
  1983. actual.(*SliceExpr).High)
  1984. require.Equal(t, expected.LBrack,
  1985. actual.(*SliceExpr).LBrack)
  1986. require.Equal(t, expected.RBrack,
  1987. actual.(*SliceExpr).RBrack)
  1988. case *SelectorExpr:
  1989. equalExpr(t, expected.Expr,
  1990. actual.(*SelectorExpr).Expr)
  1991. equalExpr(t, expected.Sel,
  1992. actual.(*SelectorExpr).Sel)
  1993. case *ImportExpr:
  1994. require.Equal(t, expected.ModuleName,
  1995. actual.(*ImportExpr).ModuleName)
  1996. require.Equal(t, int(expected.TokenPos),
  1997. int(actual.(*ImportExpr).TokenPos))
  1998. require.Equal(t, expected.Token,
  1999. actual.(*ImportExpr).Token)
  2000. case *ErrorExpr:
  2001. equalExpr(t, expected.Expr,
  2002. actual.(*ErrorExpr).Expr)
  2003. require.Equal(t, int(expected.ErrorPos),
  2004. int(actual.(*ErrorExpr).ErrorPos))
  2005. require.Equal(t, int(expected.LParen),
  2006. int(actual.(*ErrorExpr).LParen))
  2007. require.Equal(t, int(expected.RParen),
  2008. int(actual.(*ErrorExpr).RParen))
  2009. case *CondExpr:
  2010. equalExpr(t, expected.Cond,
  2011. actual.(*CondExpr).Cond)
  2012. equalExpr(t, expected.True,
  2013. actual.(*CondExpr).True)
  2014. equalExpr(t, expected.False,
  2015. actual.(*CondExpr).False)
  2016. require.Equal(t, expected.QuestionPos,
  2017. actual.(*CondExpr).QuestionPos)
  2018. require.Equal(t, expected.ColonPos,
  2019. actual.(*CondExpr).ColonPos)
  2020. default:
  2021. panic(fmt.Errorf("unknown type: %T", expected))
  2022. }
  2023. }
  2024. func equalFuncType(t *testing.T, expected, actual *FuncType) {
  2025. require.Equal(t, expected.Params.LParen, actual.Params.LParen)
  2026. require.Equal(t, expected.Params.RParen, actual.Params.RParen)
  2027. equalIdents(t, expected.Params.List, actual.Params.List)
  2028. }
  2029. func equalIdents(t *testing.T, expected, actual []*Ident) {
  2030. require.Equal(t, len(expected), len(actual))
  2031. for i := 0; i < len(expected); i++ {
  2032. equalExpr(t, expected[i], actual[i])
  2033. }
  2034. }
  2035. func equalExprs(t *testing.T, expected, actual []Expr) {
  2036. require.Equal(t, len(expected), len(actual))
  2037. for i := 0; i < len(expected); i++ {
  2038. equalExpr(t, expected[i], actual[i])
  2039. }
  2040. }
  2041. func equalStmts(t *testing.T, expected, actual []Stmt) {
  2042. require.Equal(t, len(expected), len(actual))
  2043. for i := 0; i < len(expected); i++ {
  2044. equalStmt(t, expected[i], actual[i])
  2045. }
  2046. }
  2047. func equalMapElements(
  2048. t *testing.T,
  2049. expected, actual []*MapElementLit,
  2050. ) {
  2051. require.Equal(t, len(expected), len(actual))
  2052. for i := 0; i < len(expected); i++ {
  2053. require.Equal(t, expected[i].Key, actual[i].Key)
  2054. require.Equal(t, expected[i].KeyPos, actual[i].KeyPos)
  2055. require.Equal(t, expected[i].ColonPos, actual[i].ColonPos)
  2056. equalExpr(t, expected[i].Value, actual[i].Value)
  2057. }
  2058. }
  2059. func parseSource(
  2060. filename string,
  2061. src []byte,
  2062. trace io.Writer,
  2063. ) (res *File, err error) {
  2064. fileSet := NewFileSet()
  2065. file := fileSet.AddFile(filename, -1, len(src))
  2066. p := NewParser(file, src, trace)
  2067. return p.ParseFile()
  2068. }