1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239 |
- package parser_test
- import (
- "fmt"
- "io"
- "reflect"
- "strconv"
- "strings"
- "testing"
- . "github.com/d5/tengo/v2/parser"
- "github.com/d5/tengo/v2/require"
- "github.com/d5/tengo/v2/token"
- )
- func TestParserError(t *testing.T) {
- err := &Error{Pos: SourceFilePos{
- Offset: 10, Line: 1, Column: 10,
- }, Msg: "test"}
- require.Equal(t, "Parse Error: test\n\tat 1:10", err.Error())
- }
- func TestParserErrorList(t *testing.T) {
- var list ErrorList
- list.Add(SourceFilePos{Offset: 20, Line: 2, Column: 10}, "error 2")
- list.Add(SourceFilePos{Offset: 30, Line: 3, Column: 10}, "error 3")
- list.Add(SourceFilePos{Offset: 10, Line: 1, Column: 10}, "error 1")
- list.Sort()
- require.Equal(t, "Parse Error: error 1\n\tat 1:10 (and 2 more errors)",
- list.Error())
- }
- func TestParseArray(t *testing.T) {
- expectParse(t, "[1, 2, 3]", func(p pfn) []Stmt {
- return stmts(
- exprStmt(
- arrayLit(p(1, 1), p(1, 9),
- intLit(1, p(1, 2)),
- intLit(2, p(1, 5)),
- intLit(3, p(1, 8)))))
- })
- expectParse(t, `
- [
- 1,
- 2,
- 3
- ]`, func(p pfn) []Stmt {
- return stmts(
- exprStmt(
- arrayLit(p(2, 1), p(6, 1),
- intLit(1, p(3, 2)),
- intLit(2, p(4, 2)),
- intLit(3, p(5, 2)))))
- })
- expectParse(t, `
- [
- 1,
- 2,
- 3
- ]`, func(p pfn) []Stmt {
- return stmts(
- exprStmt(
- arrayLit(p(2, 1), p(7, 1),
- intLit(1, p(3, 2)),
- intLit(2, p(4, 2)),
- intLit(3, p(5, 2)))))
- })
- expectParse(t, `[1, "foo", 12.34]`, func(p pfn) []Stmt {
- return stmts(
- exprStmt(
- arrayLit(p(1, 1), p(1, 17),
- intLit(1, p(1, 2)),
- stringLit("foo", p(1, 5)),
- floatLit(12.34, p(1, 12)))))
- })
- expectParse(t, "a = [1, 2, 3]", func(p pfn) []Stmt {
- return stmts(
- assignStmt(
- exprs(ident("a", p(1, 1))),
- exprs(arrayLit(p(1, 5), p(1, 13),
- intLit(1, p(1, 6)),
- intLit(2, p(1, 9)),
- intLit(3, p(1, 12)))),
- token.Assign,
- p(1, 3)))
- })
- expectParse(t, "a = [1 + 2, b * 4, [4, c]]", func(p pfn) []Stmt {
- return stmts(
- assignStmt(
- exprs(ident("a", p(1, 1))),
- exprs(arrayLit(p(1, 5), p(1, 26),
- binaryExpr(
- intLit(1, p(1, 6)),
- intLit(2, p(1, 10)),
- token.Add,
- p(1, 8)),
- binaryExpr(
- ident("b", p(1, 13)),
- intLit(4, p(1, 17)),
- token.Mul,
- p(1, 15)),
- arrayLit(p(1, 20), p(1, 25),
- intLit(4, p(1, 21)),
- ident("c", p(1, 24))))),
- token.Assign,
- p(1, 3)))
- })
- expectParseError(t, `[1, 2, 3,]`)
- expectParseError(t, `
- [
- 1,
- 2,
- 3,
- ]`)
- expectParseError(t, `
- [
- 1,
- 2,
- 3,
- ]`)
- expectParseError(t, `[1, 2, 3, ,]`)
- }
- func TestParseAssignment(t *testing.T) {
- expectParse(t, "a = 5", func(p pfn) []Stmt {
- return stmts(
- assignStmt(
- exprs(ident("a", p(1, 1))),
- exprs(intLit(5, p(1, 5))),
- token.Assign,
- p(1, 3)))
- })
- expectParse(t, "a := 5", func(p pfn) []Stmt {
- return stmts(
- assignStmt(
- exprs(ident("a", p(1, 1))),
- exprs(intLit(5, p(1, 6))),
- token.Define,
- p(1, 3)))
- })
- expectParse(t, "a, b = 5, 10", func(p pfn) []Stmt {
- return stmts(
- assignStmt(
- exprs(
- ident("a", p(1, 1)),
- ident("b", p(1, 4))),
- exprs(
- intLit(5, p(1, 8)),
- intLit(10, p(1, 11))),
- token.Assign,
- p(1, 6)))
- })
- expectParse(t, "a, b := 5, 10", func(p pfn) []Stmt {
- return stmts(
- assignStmt(
- exprs(
- ident("a", p(1, 1)),
- ident("b", p(1, 4))),
- exprs(
- intLit(5, p(1, 9)),
- intLit(10, p(1, 12))),
- token.Define,
- p(1, 6)))
- })
- expectParse(t, "a, b = a + 2, b - 8", func(p pfn) []Stmt {
- return stmts(
- assignStmt(
- exprs(
- ident("a", p(1, 1)),
- ident("b", p(1, 4))),
- exprs(
- binaryExpr(
- ident("a", p(1, 8)),
- intLit(2, p(1, 12)),
- token.Add,
- p(1, 10)),
- binaryExpr(
- ident("b", p(1, 15)),
- intLit(8, p(1, 19)),
- token.Sub,
- p(1, 17))),
- token.Assign,
- p(1, 6)))
- })
- expectParse(t, "a = [1, 2, 3]", func(p pfn) []Stmt {
- return stmts(
- assignStmt(
- exprs(ident("a", p(1, 1))),
- exprs(arrayLit(p(1, 5), p(1, 13),
- intLit(1, p(1, 6)),
- intLit(2, p(1, 9)),
- intLit(3, p(1, 12)))),
- token.Assign,
- p(1, 3)))
- })
- expectParse(t, "a = [1 + 2, b * 4, [4, c]]", func(p pfn) []Stmt {
- return stmts(
- assignStmt(
- exprs(ident("a", p(1, 1))),
- exprs(arrayLit(p(1, 5), p(1, 26),
- binaryExpr(
- intLit(1, p(1, 6)),
- intLit(2, p(1, 10)),
- token.Add,
- p(1, 8)),
- binaryExpr(
- ident("b", p(1, 13)),
- intLit(4, p(1, 17)),
- token.Mul,
- p(1, 15)),
- arrayLit(p(1, 20), p(1, 25),
- intLit(4, p(1, 21)),
- ident("c", p(1, 24))))),
- token.Assign,
- p(1, 3)))
- })
- expectParse(t, "a += 5", func(p pfn) []Stmt {
- return stmts(
- assignStmt(
- exprs(ident("a", p(1, 1))),
- exprs(intLit(5, p(1, 6))),
- token.AddAssign,
- p(1, 3)))
- })
- expectParse(t, "a *= 5 + 10", func(p pfn) []Stmt {
- return stmts(
- assignStmt(
- exprs(ident("a", p(1, 1))),
- exprs(
- binaryExpr(
- intLit(5, p(1, 6)),
- intLit(10, p(1, 10)),
- token.Add,
- p(1, 8))),
- token.MulAssign,
- p(1, 3)))
- })
- }
- func TestParseBoolean(t *testing.T) {
- expectParse(t, "true", func(p pfn) []Stmt {
- return stmts(
- exprStmt(
- boolLit(true, p(1, 1))))
- })
- expectParse(t, "false", func(p pfn) []Stmt {
- return stmts(
- exprStmt(
- boolLit(false, p(1, 1))))
- })
- expectParse(t, "true != false", func(p pfn) []Stmt {
- return stmts(
- exprStmt(
- binaryExpr(
- boolLit(true, p(1, 1)),
- boolLit(false, p(1, 9)),
- token.NotEqual,
- p(1, 6))))
- })
- expectParse(t, "!false", func(p pfn) []Stmt {
- return stmts(
- exprStmt(
- unaryExpr(
- boolLit(false, p(1, 2)),
- token.Not,
- p(1, 1))))
- })
- }
- func TestParseCall(t *testing.T) {
- expectParse(t, "add(1, 2, 3)", func(p pfn) []Stmt {
- return stmts(
- exprStmt(
- callExpr(
- ident("add", p(1, 1)),
- p(1, 4), p(1, 12), NoPos,
- intLit(1, p(1, 5)),
- intLit(2, p(1, 8)),
- intLit(3, p(1, 11)))))
- })
- expectParse(t, "add(1, 2, v...)", func(p pfn) []Stmt {
- return stmts(
- exprStmt(
- callExpr(
- ident("add", p(1, 1)),
- p(1, 4), p(1, 15), p(1, 12),
- intLit(1, p(1, 5)),
- intLit(2, p(1, 8)),
- ident("v", p(1, 11)))))
- })
- expectParse(t, "a = add(1, 2, 3)", func(p pfn) []Stmt {
- return stmts(
- assignStmt(
- exprs(
- ident("a", p(1, 1))),
- exprs(
- callExpr(
- ident("add", p(1, 5)),
- p(1, 8), p(1, 16), NoPos,
- intLit(1, p(1, 9)),
- intLit(2, p(1, 12)),
- intLit(3, p(1, 15)))),
- token.Assign,
- p(1, 3)))
- })
- expectParse(t, "a, b = add(1, 2, 3)", func(p pfn) []Stmt {
- return stmts(
- assignStmt(
- exprs(
- ident("a", p(1, 1)),
- ident("b", p(1, 4))),
- exprs(
- callExpr(
- ident("add", p(1, 8)),
- p(1, 11), p(1, 19), NoPos,
- intLit(1, p(1, 12)),
- intLit(2, p(1, 15)),
- intLit(3, p(1, 18)))),
- token.Assign,
- p(1, 6)))
- })
- expectParse(t, "add(a + 1, 2 * 1, (b + c))", func(p pfn) []Stmt {
- return stmts(
- exprStmt(
- callExpr(
- ident("add", p(1, 1)),
- p(1, 4), p(1, 26), NoPos,
- binaryExpr(
- ident("a", p(1, 5)),
- intLit(1, p(1, 9)),
- token.Add,
- p(1, 7)),
- binaryExpr(
- intLit(2, p(1, 12)),
- intLit(1, p(1, 16)),
- token.Mul,
- p(1, 14)),
- parenExpr(
- binaryExpr(
- ident("b", p(1, 20)),
- ident("c", p(1, 24)),
- token.Add,
- p(1, 22)),
- p(1, 19), p(1, 25)))))
- })
- expectParseString(t, "a + add(b * c) + d", "((a + add((b * c))) + d)")
- expectParseString(t, "add(a, b, 1, 2 * 3, 4 + 5, add(6, 7 * 8))",
- "add(a, b, 1, (2 * 3), (4 + 5), add(6, (7 * 8)))")
- expectParseString(t, "f1(a) + f2(b) * f3(c)", "(f1(a) + (f2(b) * f3(c)))")
- expectParseString(t, "(f1(a) + f2(b)) * f3(c)",
- "(((f1(a) + f2(b))) * f3(c))")
- expectParse(t, "func(a, b) { a + b }(1, 2)", func(p pfn) []Stmt {
- return stmts(
- exprStmt(
- callExpr(
- funcLit(
- funcType(
- identList(
- p(1, 5), p(1, 10),
- false,
- ident("a", p(1, 6)),
- ident("b", p(1, 9))),
- p(1, 1)),
- blockStmt(
- p(1, 12), p(1, 20),
- exprStmt(
- binaryExpr(
- ident("a", p(1, 14)),
- ident("b", p(1, 18)),
- token.Add,
- p(1, 16))))),
- p(1, 21), p(1, 26), NoPos,
- intLit(1, p(1, 22)),
- intLit(2, p(1, 25)))))
- })
- expectParse(t, `a.b()`, func(p pfn) []Stmt {
- return stmts(
- exprStmt(
- callExpr(
- selectorExpr(
- ident("a", p(1, 1)),
- stringLit("b", p(1, 3))),
- p(1, 4), p(1, 5), NoPos)))
- })
- expectParse(t, `a.b.c()`, func(p pfn) []Stmt {
- return stmts(
- exprStmt(
- callExpr(
- selectorExpr(
- selectorExpr(
- ident("a", p(1, 1)),
- stringLit("b", p(1, 3))),
- stringLit("c", p(1, 5))),
- p(1, 6), p(1, 7), NoPos)))
- })
- expectParse(t, `a["b"].c()`, func(p pfn) []Stmt {
- return stmts(
- exprStmt(
- callExpr(
- selectorExpr(
- indexExpr(
- ident("a", p(1, 1)),
- stringLit("b", p(1, 3)),
- p(1, 2), p(1, 6)),
- stringLit("c", p(1, 8))),
- p(1, 9), p(1, 10), NoPos)))
- })
- expectParseError(t, `add(...a, 1)`)
- expectParseError(t, `add(a..., 1)`)
- expectParseError(t, `add(a..., b...)`)
- expectParseError(t, `add(1, a..., b...)`)
- expectParseError(t, `add(...)`)
- expectParseError(t, `add(1, ...)`)
- expectParseError(t, `add(1, ..., )`)
- expectParseError(t, `add(...a)`)
- }
- func TestParseChar(t *testing.T) {
- expectParse(t, `'A'`, func(p pfn) []Stmt {
- return stmts(
- exprStmt(
- charLit('A', 1)))
- })
- expectParse(t, `'九'`, func(p pfn) []Stmt {
- return stmts(
- exprStmt(
- charLit('九', 1)))
- })
- expectParseError(t, `''`)
- expectParseError(t, `'AB'`)
- expectParseError(t, `'A九'`)
- }
- func TestParseCondExpr(t *testing.T) {
- expectParse(t, "a ? b : c", func(p pfn) []Stmt {
- return stmts(
- exprStmt(
- condExpr(
- ident("a", p(1, 1)),
- ident("b", p(1, 5)),
- ident("c", p(1, 9)),
- p(1, 3),
- p(1, 7))))
- })
- expectParse(t, `a ?
- b :
- c`, func(p pfn) []Stmt {
- return stmts(
- exprStmt(
- condExpr(
- ident("a", p(1, 1)),
- ident("b", p(1, 5)),
- ident("c", p(1, 9)),
- p(1, 3),
- p(1, 7))))
- })
- expectParseString(t, `a ? b : c`, "(a ? b : c)")
- expectParseString(t, `a + b ? c - d : e * f`,
- "((a + b) ? (c - d) : (e * f))")
- expectParseString(t, `a == b ? c + (d / e) : f ? g : h + i`,
- "((a == b) ? (c + ((d / e))) : (f ? g : (h + i)))")
- expectParseString(t, `(a + b) ? (c - d) : (e * f)`,
- "(((a + b)) ? ((c - d)) : ((e * f)))")
- expectParseString(t, `a + (b ? c : d) - e`, "((a + ((b ? c : d))) - e)")
- expectParseString(t, `a ? b ? c : d : e`, "(a ? (b ? c : d) : e)")
- expectParseString(t, `a := b ? c : d`, "a := (b ? c : d)")
- expectParseString(t, `x := a ? b ? c : d : e`,
- "x := (a ? (b ? c : d) : e)")
- // ? : should be at the end of each line if it's multi-line
- expectParseError(t, `a
- ? b
- : c`)
- expectParseError(t, `a ? (b : e)`)
- expectParseError(t, `(a ? b) : e`)
- }
- func TestParseError(t *testing.T) {
- expectParse(t, `error(1234)`, func(p pfn) []Stmt {
- return stmts(
- exprStmt(
- errorExpr(p(1, 1), intLit(1234, p(1, 7)), p(1, 6), p(1, 11))))
- })
- expectParse(t, `err1 := error("some error")`, func(p pfn) []Stmt {
- return stmts(
- assignStmt(
- exprs(ident("err1", p(1, 1))),
- exprs(errorExpr(p(1, 9),
- stringLit("some error", p(1, 15)), p(1, 14), p(1, 27))),
- token.Define, p(1, 6)))
- })
- expectParse(t, `return error("some error")`, func(p pfn) []Stmt {
- return stmts(
- returnStmt(p(1, 1),
- errorExpr(p(1, 8),
- stringLit("some error", p(1, 14)), p(1, 13), p(1, 26))))
- })
- expectParse(t, `return error("some" + "error")`, func(p pfn) []Stmt {
- return stmts(
- returnStmt(p(1, 1),
- errorExpr(p(1, 8),
- binaryExpr(
- stringLit("some", p(1, 14)),
- stringLit("error", p(1, 23)),
- token.Add, p(1, 21)),
- p(1, 13), p(1, 30))))
- })
- expectParseError(t, `error()`) // must have a value
- }
- func TestParseForIn(t *testing.T) {
- expectParse(t, "for x in y {}", func(p pfn) []Stmt {
- return stmts(
- forInStmt(
- ident("_", p(1, 5)),
- ident("x", p(1, 5)),
- ident("y", p(1, 10)),
- blockStmt(p(1, 12), p(1, 13)),
- p(1, 1)))
- })
- expectParse(t, "for _ in y {}", func(p pfn) []Stmt {
- return stmts(
- forInStmt(
- ident("_", p(1, 5)),
- ident("_", p(1, 5)),
- ident("y", p(1, 10)),
- blockStmt(p(1, 12), p(1, 13)),
- p(1, 1)))
- })
- expectParse(t, "for x in [1, 2, 3] {}", func(p pfn) []Stmt {
- return stmts(
- forInStmt(
- ident("_", p(1, 5)),
- ident("x", p(1, 5)),
- arrayLit(
- p(1, 10), p(1, 18),
- intLit(1, p(1, 11)),
- intLit(2, p(1, 14)),
- intLit(3, p(1, 17))),
- blockStmt(p(1, 20), p(1, 21)),
- p(1, 1)))
- })
- expectParse(t, "for x, y in z {}", func(p pfn) []Stmt {
- return stmts(
- forInStmt(
- ident("x", p(1, 5)),
- ident("y", p(1, 8)),
- ident("z", p(1, 13)),
- blockStmt(p(1, 15), p(1, 16)),
- p(1, 1)))
- })
- expectParse(t, "for x, y in {k1: 1, k2: 2} {}", func(p pfn) []Stmt {
- return stmts(
- forInStmt(
- ident("x", p(1, 5)),
- ident("y", p(1, 8)),
- mapLit(
- p(1, 13), p(1, 26),
- mapElementLit(
- "k1", p(1, 14), p(1, 16), intLit(1, p(1, 18))),
- mapElementLit(
- "k2", p(1, 21), p(1, 23), intLit(2, p(1, 25)))),
- blockStmt(p(1, 28), p(1, 29)),
- p(1, 1)))
- })
- }
- func TestParseFor(t *testing.T) {
- expectParse(t, "for {}", func(p pfn) []Stmt {
- return stmts(
- forStmt(nil, nil, nil, blockStmt(p(1, 5), p(1, 6)), p(1, 1)))
- })
- expectParse(t, "for a == 5 {}", func(p pfn) []Stmt {
- return stmts(
- forStmt(
- nil,
- binaryExpr(
- ident("a", p(1, 5)),
- intLit(5, p(1, 10)),
- token.Equal,
- p(1, 7)),
- nil,
- blockStmt(p(1, 12), p(1, 13)),
- p(1, 1)))
- })
- expectParse(t, "for a := 0; a == 5; {}", func(p pfn) []Stmt {
- return stmts(
- forStmt(
- assignStmt(
- exprs(ident("a", p(1, 5))),
- exprs(intLit(0, p(1, 10))),
- token.Define, p(1, 7)),
- binaryExpr(
- ident("a", p(1, 13)),
- intLit(5, p(1, 18)),
- token.Equal,
- p(1, 15)),
- nil,
- blockStmt(p(1, 22), p(1, 23)),
- p(1, 1)))
- })
- expectParse(t, "for a := 0; a < 5; a++ {}", func(p pfn) []Stmt {
- return stmts(
- forStmt(
- assignStmt(
- exprs(ident("a", p(1, 5))),
- exprs(intLit(0, p(1, 10))),
- token.Define, p(1, 7)),
- binaryExpr(
- ident("a", p(1, 13)),
- intLit(5, p(1, 17)),
- token.Less,
- p(1, 15)),
- incDecStmt(
- ident("a", p(1, 20)),
- token.Inc, p(1, 21)),
- blockStmt(p(1, 24), p(1, 25)),
- p(1, 1)))
- })
- expectParse(t, "for ; a < 5; a++ {}", func(p pfn) []Stmt {
- return stmts(
- forStmt(
- nil,
- binaryExpr(
- ident("a", p(1, 7)),
- intLit(5, p(1, 11)),
- token.Less,
- p(1, 9)),
- incDecStmt(
- ident("a", p(1, 14)),
- token.Inc, p(1, 15)),
- blockStmt(p(1, 18), p(1, 19)),
- p(1, 1)))
- })
- expectParse(t, "for a := 0; ; a++ {}", func(p pfn) []Stmt {
- return stmts(
- forStmt(
- assignStmt(
- exprs(ident("a", p(1, 5))),
- exprs(intLit(0, p(1, 10))),
- token.Define, p(1, 7)),
- nil,
- incDecStmt(
- ident("a", p(1, 15)),
- token.Inc, p(1, 16)),
- blockStmt(p(1, 19), p(1, 20)),
- p(1, 1)))
- })
- expectParse(t, "for a == 5 && b != 4 {}", func(p pfn) []Stmt {
- return stmts(
- forStmt(
- nil,
- binaryExpr(
- binaryExpr(
- ident("a", p(1, 5)),
- intLit(5, p(1, 10)),
- token.Equal,
- p(1, 7)),
- binaryExpr(
- ident("b", p(1, 15)),
- intLit(4, p(1, 20)),
- token.NotEqual,
- p(1, 17)),
- token.LAnd,
- p(1, 12)),
- nil,
- blockStmt(p(1, 22), p(1, 23)),
- p(1, 1)))
- })
- }
- func TestParseFunction(t *testing.T) {
- expectParse(t, "a = func(b, c, d) { return d }", func(p pfn) []Stmt {
- return stmts(
- assignStmt(
- exprs(
- ident("a", p(1, 1))),
- exprs(
- funcLit(
- funcType(
- identList(p(1, 9), p(1, 17), false,
- ident("b", p(1, 10)),
- ident("c", p(1, 13)),
- ident("d", p(1, 16))),
- p(1, 5)),
- blockStmt(p(1, 19), p(1, 30),
- returnStmt(p(1, 21), ident("d", p(1, 28)))))),
- token.Assign,
- p(1, 3)))
- })
- }
- func TestParseVariadicFunction(t *testing.T) {
- expectParse(t, "a = func(...args) { return args }", func(p pfn) []Stmt {
- return stmts(
- assignStmt(
- exprs(
- ident("a", p(1, 1))),
- exprs(
- funcLit(
- funcType(
- identList(
- p(1, 9), p(1, 17),
- true,
- ident("args", p(1, 13)),
- ), p(1, 5)),
- blockStmt(p(1, 19), p(1, 33),
- returnStmt(p(1, 21),
- ident("args", p(1, 28)),
- ),
- ),
- ),
- ),
- token.Assign,
- p(1, 3)))
- })
- }
- func TestParseVariadicFunctionWithArgs(t *testing.T) {
- expectParse(t, "a = func(x, y, ...z) { return z }", func(p pfn) []Stmt {
- return stmts(
- assignStmt(
- exprs(
- ident("a", p(1, 1))),
- exprs(
- funcLit(
- funcType(
- identList(
- p(1, 9), p(1, 20),
- true,
- ident("x", p(1, 10)),
- ident("y", p(1, 13)),
- ident("z", p(1, 19)),
- ), p(1, 5)),
- blockStmt(p(1, 22), p(1, 33),
- returnStmt(p(1, 24),
- ident("z", p(1, 31)),
- ),
- ),
- ),
- ),
- token.Assign,
- p(1, 3)))
- })
- expectParseError(t, "a = func(x, y, ...z, invalid) { return z }")
- expectParseError(t, "a = func(...args, invalid) { return args }")
- }
- func TestParseIf(t *testing.T) {
- expectParse(t, "if a == 5 {}", func(p pfn) []Stmt {
- return stmts(
- ifStmt(
- nil,
- binaryExpr(
- ident("a", p(1, 4)),
- intLit(5, p(1, 9)),
- token.Equal,
- p(1, 6)),
- blockStmt(
- p(1, 11), p(1, 12)),
- nil,
- p(1, 1)))
- })
- expectParse(t, "if a == 5 && b != 3 {}", func(p pfn) []Stmt {
- return stmts(
- ifStmt(
- nil,
- binaryExpr(
- binaryExpr(
- ident("a", p(1, 4)),
- intLit(5, p(1, 9)),
- token.Equal,
- p(1, 6)),
- binaryExpr(
- ident("b", p(1, 14)),
- intLit(3, p(1, 19)),
- token.NotEqual,
- p(1, 16)),
- token.LAnd,
- p(1, 11)),
- blockStmt(
- p(1, 21), p(1, 22)),
- nil,
- p(1, 1)))
- })
- expectParse(t, "if a == 5 { a = 3; a = 1 }", func(p pfn) []Stmt {
- return stmts(
- ifStmt(
- nil,
- binaryExpr(
- ident("a", p(1, 4)),
- intLit(5, p(1, 9)),
- token.Equal,
- p(1, 6)),
- blockStmt(
- p(1, 11), p(1, 26),
- assignStmt(
- exprs(ident("a", p(1, 13))),
- exprs(intLit(3, p(1, 17))),
- token.Assign,
- p(1, 15)),
- assignStmt(
- exprs(ident("a", p(1, 20))),
- exprs(intLit(1, p(1, 24))),
- token.Assign,
- p(1, 22))),
- nil,
- p(1, 1)))
- })
- expectParse(t, "if a == 5 { a = 3; a = 1 } else { a = 2; a = 4 }",
- func(p pfn) []Stmt {
- return stmts(
- ifStmt(
- nil,
- binaryExpr(
- ident("a", p(1, 4)),
- intLit(5, p(1, 9)),
- token.Equal,
- p(1, 6)),
- blockStmt(
- p(1, 11), p(1, 26),
- assignStmt(
- exprs(ident("a", p(1, 13))),
- exprs(intLit(3, p(1, 17))),
- token.Assign,
- p(1, 15)),
- assignStmt(
- exprs(ident("a", p(1, 20))),
- exprs(intLit(1, p(1, 24))),
- token.Assign,
- p(1, 22))),
- blockStmt(
- p(1, 33), p(1, 48),
- assignStmt(
- exprs(ident("a", p(1, 35))),
- exprs(intLit(2, p(1, 39))),
- token.Assign,
- p(1, 37)),
- assignStmt(
- exprs(ident("a", p(1, 42))),
- exprs(intLit(4, p(1, 46))),
- token.Assign,
- p(1, 44))),
- p(1, 1)))
- })
- expectParse(t, `
- if a == 5 {
- b = 3
- c = 1
- } else if d == 3 {
- e = 8
- f = 3
- } else {
- g = 2
- h = 4
- }`, func(p pfn) []Stmt {
- return stmts(
- ifStmt(
- nil,
- binaryExpr(
- ident("a", p(2, 4)),
- intLit(5, p(2, 9)),
- token.Equal,
- p(2, 6)),
- blockStmt(
- p(2, 11), p(5, 1),
- assignStmt(
- exprs(ident("b", p(3, 2))),
- exprs(intLit(3, p(3, 6))),
- token.Assign,
- p(3, 4)),
- assignStmt(
- exprs(ident("c", p(4, 2))),
- exprs(intLit(1, p(4, 6))),
- token.Assign,
- p(4, 4))),
- ifStmt(
- nil,
- binaryExpr(
- ident("d", p(5, 11)),
- intLit(3, p(5, 16)),
- token.Equal,
- p(5, 13)),
- blockStmt(
- p(5, 18), p(8, 1),
- assignStmt(
- exprs(ident("e", p(6, 2))),
- exprs(intLit(8, p(6, 6))),
- token.Assign,
- p(6, 4)),
- assignStmt(
- exprs(ident("f", p(7, 2))),
- exprs(intLit(3, p(7, 6))),
- token.Assign,
- p(7, 4))),
- blockStmt(
- p(8, 8), p(11, 1),
- assignStmt(
- exprs(ident("g", p(9, 2))),
- exprs(intLit(2, p(9, 6))),
- token.Assign,
- p(9, 4)),
- assignStmt(
- exprs(ident("h", p(10, 2))),
- exprs(intLit(4, p(10, 6))),
- token.Assign,
- p(10, 4))),
- p(5, 8)),
- p(2, 1)))
- })
- expectParse(t, "if a := 3; a < b {}", func(p pfn) []Stmt {
- return stmts(
- ifStmt(
- assignStmt(
- exprs(ident("a", p(1, 4))),
- exprs(intLit(3, p(1, 9))),
- token.Define, p(1, 6)),
- binaryExpr(
- ident("a", p(1, 12)),
- ident("b", p(1, 16)),
- token.Less, p(1, 14)),
- blockStmt(
- p(1, 18), p(1, 19)),
- nil,
- p(1, 1)))
- })
- expectParse(t, "if a++; a < b {}", func(p pfn) []Stmt {
- return stmts(
- ifStmt(
- incDecStmt(ident("a", p(1, 4)), token.Inc, p(1, 5)),
- binaryExpr(
- ident("a", p(1, 9)),
- ident("b", p(1, 13)),
- token.Less, p(1, 11)),
- blockStmt(
- p(1, 15), p(1, 16)),
- nil,
- p(1, 1)))
- })
- expectParseError(t, `if {}`)
- expectParseError(t, `if a == b { } else a != b { }`)
- expectParseError(t, `if a == b { } else if { }`)
- expectParseError(t, `else { }`)
- expectParseError(t, `if ; {}`)
- expectParseError(t, `if a := 3; {}`)
- expectParseError(t, `if ; a < 3 {}`)
- }
- func TestParseImport(t *testing.T) {
- expectParse(t, `a := import("mod1")`, func(p pfn) []Stmt {
- return stmts(
- assignStmt(
- exprs(ident("a", p(1, 1))),
- exprs(importExpr("mod1", p(1, 6))),
- token.Define, p(1, 3)))
- })
- expectParse(t, `import("mod1").var1`, func(p pfn) []Stmt {
- return stmts(
- exprStmt(
- selectorExpr(
- importExpr("mod1", p(1, 1)),
- stringLit("var1", p(1, 16)))))
- })
- expectParse(t, `import("mod1").func1()`, func(p pfn) []Stmt {
- return stmts(
- exprStmt(
- callExpr(
- selectorExpr(
- importExpr("mod1", p(1, 1)),
- stringLit("func1", p(1, 16))),
- p(1, 21), p(1, 22), NoPos)))
- })
- expectParse(t, `for x, y in import("mod1") {}`, func(p pfn) []Stmt {
- return stmts(
- forInStmt(
- ident("x", p(1, 5)),
- ident("y", p(1, 8)),
- importExpr("mod1", p(1, 13)),
- blockStmt(p(1, 28), p(1, 29)),
- p(1, 1)))
- })
- }
- func TestParseIndex(t *testing.T) {
- expectParse(t, "[1, 2, 3][1]", func(p pfn) []Stmt {
- return stmts(
- exprStmt(
- indexExpr(
- arrayLit(p(1, 1), p(1, 9),
- intLit(1, p(1, 2)),
- intLit(2, p(1, 5)),
- intLit(3, p(1, 8))),
- intLit(1, p(1, 11)),
- p(1, 10), p(1, 12))))
- })
- expectParse(t, "[1, 2, 3][5 - a]", func(p pfn) []Stmt {
- return stmts(
- exprStmt(
- indexExpr(
- arrayLit(p(1, 1), p(1, 9),
- intLit(1, p(1, 2)),
- intLit(2, p(1, 5)),
- intLit(3, p(1, 8))),
- binaryExpr(
- intLit(5, p(1, 11)),
- ident("a", p(1, 15)),
- token.Sub,
- p(1, 13)),
- p(1, 10), p(1, 16))))
- })
- expectParse(t, "[1, 2, 3][5 : a]", func(p pfn) []Stmt {
- return stmts(
- exprStmt(
- sliceExpr(
- arrayLit(p(1, 1), p(1, 9),
- intLit(1, p(1, 2)),
- intLit(2, p(1, 5)),
- intLit(3, p(1, 8))),
- intLit(5, p(1, 11)),
- ident("a", p(1, 15)),
- p(1, 10), p(1, 16))))
- })
- expectParse(t, "[1, 2, 3][a + 3 : b - 8]", func(p pfn) []Stmt {
- return stmts(
- exprStmt(
- sliceExpr(
- arrayLit(p(1, 1), p(1, 9),
- intLit(1, p(1, 2)),
- intLit(2, p(1, 5)),
- intLit(3, p(1, 8))),
- binaryExpr(
- ident("a", p(1, 11)),
- intLit(3, p(1, 15)),
- token.Add,
- p(1, 13)),
- binaryExpr(
- ident("b", p(1, 19)),
- intLit(8, p(1, 23)),
- token.Sub,
- p(1, 21)),
- p(1, 10), p(1, 24))))
- })
- expectParse(t, `{a: 1, b: 2}["b"]`, func(p pfn) []Stmt {
- return stmts(
- exprStmt(
- indexExpr(
- mapLit(p(1, 1), p(1, 12),
- mapElementLit(
- "a", p(1, 2), p(1, 3), intLit(1, p(1, 5))),
- mapElementLit(
- "b", p(1, 8), p(1, 9), intLit(2, p(1, 11)))),
- stringLit("b", p(1, 14)),
- p(1, 13), p(1, 17))))
- })
- expectParse(t, `{a: 1, b: 2}[a + b]`, func(p pfn) []Stmt {
- return stmts(
- exprStmt(
- indexExpr(
- mapLit(p(1, 1), p(1, 12),
- mapElementLit(
- "a", p(1, 2), p(1, 3), intLit(1, p(1, 5))),
- mapElementLit(
- "b", p(1, 8), p(1, 9), intLit(2, p(1, 11)))),
- binaryExpr(
- ident("a", p(1, 14)),
- ident("b", p(1, 18)),
- token.Add,
- p(1, 16)),
- p(1, 13), p(1, 19))))
- })
- }
- func TestParseLogical(t *testing.T) {
- expectParse(t, "a && 5 || true", func(p pfn) []Stmt {
- return stmts(
- exprStmt(
- binaryExpr(
- binaryExpr(
- ident("a", p(1, 1)),
- intLit(5, p(1, 6)),
- token.LAnd,
- p(1, 3)),
- boolLit(true, p(1, 11)),
- token.LOr,
- p(1, 8))))
- })
- expectParse(t, "a || 5 && true", func(p pfn) []Stmt {
- return stmts(
- exprStmt(
- binaryExpr(
- ident("a", p(1, 1)),
- binaryExpr(
- intLit(5, p(1, 6)),
- boolLit(true, p(1, 11)),
- token.LAnd,
- p(1, 8)),
- token.LOr,
- p(1, 3))))
- })
- expectParse(t, "a && (5 || true)", func(p pfn) []Stmt {
- return stmts(
- exprStmt(
- binaryExpr(
- ident("a", p(1, 1)),
- parenExpr(
- binaryExpr(
- intLit(5, p(1, 7)),
- boolLit(true, p(1, 12)),
- token.LOr,
- p(1, 9)),
- p(1, 6), p(1, 16)),
- token.LAnd,
- p(1, 3))))
- })
- }
- func TestParseMap(t *testing.T) {
- expectParse(t, "{ key1: 1, key2: \"2\", key3: true }", func(p pfn) []Stmt {
- return stmts(
- exprStmt(
- mapLit(p(1, 1), p(1, 34),
- mapElementLit(
- "key1", p(1, 3), p(1, 7), intLit(1, p(1, 9))),
- mapElementLit(
- "key2", p(1, 12), p(1, 16), stringLit("2", p(1, 18))),
- mapElementLit(
- "key3", p(1, 23), p(1, 27), boolLit(true, p(1, 29))))))
- })
- expectParse(t, "{ \"key1\": 1 }", func(p pfn) []Stmt {
- return stmts(
- exprStmt(
- mapLit(p(1, 1), p(1, 13),
- mapElementLit(
- "key1", p(1, 3), p(1, 9), intLit(1, p(1, 11))))))
- })
- expectParse(t, "a = { key1: 1, key2: \"2\", key3: true }",
- func(p pfn) []Stmt {
- return stmts(assignStmt(
- exprs(ident("a", p(1, 1))),
- exprs(mapLit(p(1, 5), p(1, 38),
- mapElementLit(
- "key1", p(1, 7), p(1, 11), intLit(1, p(1, 13))),
- mapElementLit(
- "key2", p(1, 16), p(1, 20), stringLit("2", p(1, 22))),
- mapElementLit(
- "key3", p(1, 27), p(1, 31), boolLit(true, p(1, 33))))),
- token.Assign,
- p(1, 3)))
- })
- expectParse(t, "a = { key1: 1, key2: \"2\", key3: { k1: `bar`, k2: 4 } }",
- func(p pfn) []Stmt {
- return stmts(assignStmt(
- exprs(ident("a", p(1, 1))),
- exprs(mapLit(p(1, 5), p(1, 54),
- mapElementLit(
- "key1", p(1, 7), p(1, 11), intLit(1, p(1, 13))),
- mapElementLit(
- "key2", p(1, 16), p(1, 20), stringLit("2", p(1, 22))),
- mapElementLit(
- "key3", p(1, 27), p(1, 31),
- mapLit(p(1, 33), p(1, 52),
- mapElementLit(
- "k1", p(1, 35),
- p(1, 37), stringLit("bar", p(1, 39))),
- mapElementLit(
- "k2", p(1, 46),
- p(1, 48), intLit(4, p(1, 50))))))),
- token.Assign,
- p(1, 3)))
- })
- expectParse(t, `
- {
- key1: 1,
- key2: "2",
- key3: true
- }`, func(p pfn) []Stmt {
- return stmts(exprStmt(
- mapLit(p(2, 1), p(6, 1),
- mapElementLit(
- "key1", p(3, 2), p(3, 6), intLit(1, p(3, 8))),
- mapElementLit(
- "key2", p(4, 2), p(4, 6), stringLit("2", p(4, 8))),
- mapElementLit(
- "key3", p(5, 2), p(5, 6), boolLit(true, p(5, 8))))))
- })
- expectParseError(t, `
- {
- key1: 1,
- key2: "2",
- key3: true,
- }`) // unlike Go, trailing comma for the last element is illegal
- expectParseError(t, `{ key1: 1, }`)
- expectParseError(t, `{
- key1: 1,
- key2: 2,
- }`)
- }
- func TestParsePrecedence(t *testing.T) {
- expectParseString(t, `a + b + c`, `((a + b) + c)`)
- expectParseString(t, `a + b * c`, `(a + (b * c))`)
- expectParseString(t, `x = 2 * 1 + 3 / 4`, `x = ((2 * 1) + (3 / 4))`)
- }
- func TestParseSelector(t *testing.T) {
- expectParse(t, "a.b", func(p pfn) []Stmt {
- return stmts(
- exprStmt(
- selectorExpr(
- ident("a", p(1, 1)),
- stringLit("b", p(1, 3)))))
- })
- expectParse(t, "a.b.c", func(p pfn) []Stmt {
- return stmts(
- exprStmt(
- selectorExpr(
- selectorExpr(
- ident("a", p(1, 1)),
- stringLit("b", p(1, 3))),
- stringLit("c", p(1, 5)))))
- })
- expectParse(t, "{k1:1}.k1", func(p pfn) []Stmt {
- return stmts(
- exprStmt(
- selectorExpr(
- mapLit(
- p(1, 1), p(1, 6),
- mapElementLit(
- "k1", p(1, 2), p(1, 4), intLit(1, p(1, 5)))),
- stringLit("k1", p(1, 8)))))
- })
- expectParse(t, "{k1:{v1:1}}.k1.v1", func(p pfn) []Stmt {
- return stmts(
- exprStmt(
- selectorExpr(
- selectorExpr(
- mapLit(
- p(1, 1), p(1, 11),
- mapElementLit("k1", p(1, 2), p(1, 4),
- mapLit(p(1, 5), p(1, 10),
- mapElementLit(
- "v1", p(1, 6),
- p(1, 8), intLit(1, p(1, 9)))))),
- stringLit("k1", p(1, 13))),
- stringLit("v1", p(1, 16)))))
- })
- expectParse(t, "a.b = 4", func(p pfn) []Stmt {
- return stmts(
- assignStmt(
- exprs(
- selectorExpr(
- ident("a", p(1, 1)),
- stringLit("b", p(1, 3)))),
- exprs(intLit(4, p(1, 7))),
- token.Assign, p(1, 5)))
- })
- expectParse(t, "a.b.c = 4", func(p pfn) []Stmt {
- return stmts(
- assignStmt(
- exprs(
- selectorExpr(
- selectorExpr(
- ident("a", p(1, 1)),
- stringLit("b", p(1, 3))),
- stringLit("c", p(1, 5)))),
- exprs(intLit(4, p(1, 9))),
- token.Assign, p(1, 7)))
- })
- expectParse(t, "a.b.c = 4 + 5", func(p pfn) []Stmt {
- return stmts(
- assignStmt(
- exprs(
- selectorExpr(
- selectorExpr(
- ident("a", p(1, 1)),
- stringLit("b", p(1, 3))),
- stringLit("c", p(1, 5)))),
- exprs(
- binaryExpr(
- intLit(4, p(1, 9)),
- intLit(5, p(1, 13)),
- token.Add,
- p(1, 11))),
- token.Assign, p(1, 7)))
- })
- expectParse(t, "a[0].c = 4", func(p pfn) []Stmt {
- return stmts(
- assignStmt(
- exprs(
- selectorExpr(
- indexExpr(
- ident("a", p(1, 1)),
- intLit(0, p(1, 3)),
- p(1, 2), p(1, 4)),
- stringLit("c", p(1, 6)))),
- exprs(intLit(4, p(1, 10))),
- token.Assign, p(1, 8)))
- })
- expectParse(t, "a.b[0].c = 4", func(p pfn) []Stmt {
- return stmts(
- assignStmt(
- exprs(
- selectorExpr(
- indexExpr(
- selectorExpr(
- ident("a", p(1, 1)),
- stringLit("b", p(1, 3))),
- intLit(0, p(1, 5)),
- p(1, 4), p(1, 6)),
- stringLit("c", p(1, 8)))),
- exprs(intLit(4, p(1, 12))),
- token.Assign, p(1, 10)))
- })
- expectParse(t, "a.b[0][2].c = 4", func(p pfn) []Stmt {
- return stmts(
- assignStmt(
- exprs(
- selectorExpr(
- indexExpr(
- indexExpr(
- selectorExpr(
- ident("a", p(1, 1)),
- stringLit("b", p(1, 3))),
- intLit(0, p(1, 5)),
- p(1, 4), p(1, 6)),
- intLit(2, p(1, 8)),
- p(1, 7), p(1, 9)),
- stringLit("c", p(1, 11)))),
- exprs(intLit(4, p(1, 15))),
- token.Assign, p(1, 13)))
- })
- expectParse(t, `a.b["key1"][2].c = 4`, func(p pfn) []Stmt {
- return stmts(
- assignStmt(
- exprs(
- selectorExpr(
- indexExpr(
- indexExpr(
- selectorExpr(
- ident("a", p(1, 1)),
- stringLit("b", p(1, 3))),
- stringLit("key1", p(1, 5)),
- p(1, 4), p(1, 11)),
- intLit(2, p(1, 13)),
- p(1, 12), p(1, 14)),
- stringLit("c", p(1, 16)))),
- exprs(intLit(4, p(1, 20))),
- token.Assign, p(1, 18)))
- })
- expectParse(t, "a[0].b[2].c = 4", func(p pfn) []Stmt {
- return stmts(
- assignStmt(
- exprs(
- selectorExpr(
- indexExpr(
- selectorExpr(
- indexExpr(
- ident("a", p(1, 1)),
- intLit(0, p(1, 3)),
- p(1, 2), p(1, 4)),
- stringLit("b", p(1, 6))),
- intLit(2, p(1, 8)),
- p(1, 7), p(1, 9)),
- stringLit("c", p(1, 11)))),
- exprs(intLit(4, p(1, 15))),
- token.Assign, p(1, 13)))
- })
- expectParseError(t, `a.(b.c)`)
- }
- func TestParseSemicolon(t *testing.T) {
- expectParse(t, "1", func(p pfn) []Stmt {
- return stmts(
- exprStmt(intLit(1, p(1, 1))))
- })
- expectParse(t, "1;", func(p pfn) []Stmt {
- return stmts(
- exprStmt(intLit(1, p(1, 1))))
- })
- expectParse(t, "1;;", func(p pfn) []Stmt {
- return stmts(
- exprStmt(intLit(1, p(1, 1))),
- emptyStmt(false, p(1, 3)))
- })
- expectParse(t, `1
- `, func(p pfn) []Stmt {
- return stmts(
- exprStmt(intLit(1, p(1, 1))))
- })
- expectParse(t, `1
- ;`, func(p pfn) []Stmt {
- return stmts(
- exprStmt(intLit(1, p(1, 1))),
- emptyStmt(false, p(2, 1)))
- })
- expectParse(t, `1;
- ;`, func(p pfn) []Stmt {
- return stmts(
- exprStmt(intLit(1, p(1, 1))),
- emptyStmt(false, p(2, 1)))
- })
- }
- func TestParseString(t *testing.T) {
- expectParse(t, `a = "foo\nbar"`, func(p pfn) []Stmt {
- return stmts(
- assignStmt(
- exprs(ident("a", p(1, 1))),
- exprs(stringLit("foo\nbar", p(1, 5))),
- token.Assign,
- p(1, 3)))
- })
- expectParse(t, "a = `raw string`", func(p pfn) []Stmt {
- return stmts(
- assignStmt(
- exprs(ident("a", p(1, 1))),
- exprs(stringLit("raw string", p(1, 5))),
- token.Assign,
- p(1, 3)))
- })
- }
- func TestParseInt(t *testing.T) {
- testCases := []string{
- // All valid digits
- "1234567890",
- "0b10",
- "0o12345670",
- "0x123456789abcdef0",
- "0x123456789ABCDEF0",
- // Alternative base prefixes
- "010",
- "0B10",
- "0O10",
- "0X10",
- // Invalid digits
- "0b2",
- "08",
- "0o8",
- "1a",
- "0xg",
- // Range errors
- "9223372036854775807",
- "9223372036854775808", // invalid: range error
- // Examples from specification (https://go.dev/ref/spec#Integer_literals)
- "42",
- "4_2",
- "0600",
- "0_600",
- "0o600",
- "0O600", // second character is capital letter 'O'
- "0xBadFace",
- "0xBad_Face",
- "0x_67_7a_2f_cc_40_c6",
- "170141183460469231731687303715884105727",
- "170_141183_460469_231731_687303_715884_105727",
- "42_", // invalid: _ must separate successive digits
- "4__2", // invalid: only one _ at a time
- "0_xBadFace", // invalid: _ must separate successive digits
- }
- for _, num := range testCases {
- t.Run(num, func(t *testing.T) {
- expected, err := strconv.ParseInt(num, 0, 64)
- if err == nil {
- expectParse(t, num, func(p pfn) []Stmt {
- return stmts(exprStmt(intLit(expected, p(1, 1))))
- })
- } else {
- expectParseError(t, num)
- }
- })
- }
- }
- func TestParseFloat(t *testing.T) {
- testCases := []string{
- // Different placements of decimal point
- ".0",
- "0.",
- "0.0",
- "00.0",
- "00.00",
- "0.0.0",
- "0..0",
- // Ignoring leading zeros
- "010.0",
- "00010.0",
- "08.0",
- "0a.0", // ivalid: hex character
- // Exponents
- "1e1",
- "1E1",
- "1e1.1",
- "1e+1",
- "1e-1",
- "1e+-1",
- "0x1p1",
- "0x10p1",
- // Examples from language specifcation (https://go.dev/ref/spec#Floating-point_literals)
- "0.",
- "72.40",
- "072.40", // == 72.40
- "2.71828",
- "1.e+0",
- "6.67428e-11",
- "1E6",
- ".25",
- ".12345E+5",
- "1_5.", // == 15.0
- "0.15e+0_2", // == 15.0
- "0x1p-2", // == 0.25
- "0x2.p10", // == 2048.0
- "0x1.Fp+0", // == 1.9375
- "0X.8p-0", // == 0.5
- "0X_1FFFP-16", // == 0.1249847412109375
- "0x.p1", // invalid: mantissa has no digits
- "1p-2", // invalid: p exponent requires hexadecimal mantissa
- "0x1.5e-2", // invalid: hexadecimal mantissa requires p exponent
- "1_.5", // invalid: _ must separate successive digits
- "1._5", // invalid: _ must separate successive digits
- "1.5_e1", // invalid: _ must separate successive digits
- "1.5e_1", // invalid: _ must separate successive digits
- "1.5e1_", // invalid: _ must separate successive digits
- }
- for _, num := range testCases {
- t.Run(num, func(t *testing.T) {
- expected, err := strconv.ParseFloat(num, 64)
- if err == nil {
- expectParse(t, num, func(p pfn) []Stmt {
- return stmts(exprStmt(floatLit(expected, p(1, 1))))
- })
- } else {
- expectParseError(t, num)
- }
- })
- }
- }
- func TestParseNumberExpressions(t *testing.T) {
- expectParse(t, `0x15e+2`, func(p pfn) []Stmt {
- return stmts(
- exprStmt(
- binaryExpr(
- intLit(0x15e, p(1, 1)),
- intLit(2, p(1, 7)),
- token.Add,
- p(1, 6))))
- })
- expectParse(t, `0-_42`, func(p pfn) []Stmt {
- return stmts(
- exprStmt(
- binaryExpr(
- intLit(0, p(1, 1)),
- ident("_42", p(1, 3)),
- token.Sub,
- p(1, 2))))
- })
- }
- type pfn func(int, int) Pos // position conversion function
- type expectedFn func(pos pfn) []Stmt // callback function to return expected results
- type parseTracer struct {
- out []string
- }
- func (o *parseTracer) Write(p []byte) (n int, err error) {
- o.out = append(o.out, string(p))
- return len(p), nil
- }
- //type slowPrinter struct {
- //}
- //
- //func (o *slowPrinter) Write(p []byte) (n int, err error) {
- // fmt.Print(string(p))
- // time.Sleep(25 * time.Millisecond)
- // return len(p), nil
- //}
- func expectParse(t *testing.T, input string, fn expectedFn) {
- testFileSet := NewFileSet()
- testFile := testFileSet.AddFile("test", -1, len(input))
- var ok bool
- defer func() {
- if !ok {
- // print trace
- tr := &parseTracer{}
- p := NewParser(testFile, []byte(input), tr)
- actual, _ := p.ParseFile()
- if actual != nil {
- t.Logf("Parsed:\n%s", actual.String())
- }
- t.Logf("Trace:\n%s", strings.Join(tr.out, ""))
- }
- }()
- p := NewParser(testFile, []byte(input), nil)
- actual, err := p.ParseFile()
- require.NoError(t, err)
- expected := fn(func(line, column int) Pos {
- return Pos(int(testFile.LineStart(line)) + (column - 1))
- })
- require.Equal(t, len(expected), len(actual.Stmts))
- for i := 0; i < len(expected); i++ {
- equalStmt(t, expected[i], actual.Stmts[i])
- }
- ok = true
- }
- func expectParseError(t *testing.T, input string) {
- testFileSet := NewFileSet()
- testFile := testFileSet.AddFile("test", -1, len(input))
- var ok bool
- defer func() {
- if !ok {
- // print trace
- tr := &parseTracer{}
- p := NewParser(testFile, []byte(input), tr)
- _, _ = p.ParseFile()
- t.Logf("Trace:\n%s", strings.Join(tr.out, ""))
- }
- }()
- p := NewParser(testFile, []byte(input), nil)
- _, err := p.ParseFile()
- require.Error(t, err)
- ok = true
- }
- func expectParseString(t *testing.T, input, expected string) {
- var ok bool
- defer func() {
- if !ok {
- // print trace
- tr := &parseTracer{}
- _, _ = parseSource("test", []byte(input), tr)
- t.Logf("Trace:\n%s", strings.Join(tr.out, ""))
- }
- }()
- actual, err := parseSource("test", []byte(input), nil)
- require.NoError(t, err)
- require.Equal(t, expected, actual.String())
- ok = true
- }
- func stmts(s ...Stmt) []Stmt {
- return s
- }
- func exprStmt(x Expr) *ExprStmt {
- return &ExprStmt{Expr: x}
- }
- func assignStmt(
- lhs, rhs []Expr,
- token token.Token,
- pos Pos,
- ) *AssignStmt {
- return &AssignStmt{LHS: lhs, RHS: rhs, Token: token, TokenPos: pos}
- }
- func emptyStmt(implicit bool, pos Pos) *EmptyStmt {
- return &EmptyStmt{Implicit: implicit, Semicolon: pos}
- }
- func returnStmt(pos Pos, result Expr) *ReturnStmt {
- return &ReturnStmt{Result: result, ReturnPos: pos}
- }
- func forStmt(
- init Stmt,
- cond Expr,
- post Stmt,
- body *BlockStmt,
- pos Pos,
- ) *ForStmt {
- return &ForStmt{
- Cond: cond, Init: init, Post: post, Body: body, ForPos: pos,
- }
- }
- func forInStmt(
- key, value *Ident,
- seq Expr,
- body *BlockStmt,
- pos Pos,
- ) *ForInStmt {
- return &ForInStmt{
- Key: key, Value: value, Iterable: seq, Body: body, ForPos: pos,
- }
- }
- func ifStmt(
- init Stmt,
- cond Expr,
- body *BlockStmt,
- elseStmt Stmt,
- pos Pos,
- ) *IfStmt {
- return &IfStmt{
- Init: init, Cond: cond, Body: body, Else: elseStmt, IfPos: pos,
- }
- }
- func incDecStmt(
- expr Expr,
- tok token.Token,
- pos Pos,
- ) *IncDecStmt {
- return &IncDecStmt{Expr: expr, Token: tok, TokenPos: pos}
- }
- func funcType(params *IdentList, pos Pos) *FuncType {
- return &FuncType{Params: params, FuncPos: pos}
- }
- func blockStmt(lbrace, rbrace Pos, list ...Stmt) *BlockStmt {
- return &BlockStmt{Stmts: list, LBrace: lbrace, RBrace: rbrace}
- }
- func ident(name string, pos Pos) *Ident {
- return &Ident{Name: name, NamePos: pos}
- }
- func identList(
- opening, closing Pos,
- varArgs bool,
- list ...*Ident,
- ) *IdentList {
- return &IdentList{
- VarArgs: varArgs, List: list, LParen: opening, RParen: closing,
- }
- }
- func binaryExpr(
- x, y Expr,
- op token.Token,
- pos Pos,
- ) *BinaryExpr {
- return &BinaryExpr{LHS: x, RHS: y, Token: op, TokenPos: pos}
- }
- func condExpr(
- cond, trueExpr, falseExpr Expr,
- questionPos, colonPos Pos,
- ) *CondExpr {
- return &CondExpr{
- Cond: cond, True: trueExpr, False: falseExpr,
- QuestionPos: questionPos, ColonPos: colonPos,
- }
- }
- func unaryExpr(x Expr, op token.Token, pos Pos) *UnaryExpr {
- return &UnaryExpr{Expr: x, Token: op, TokenPos: pos}
- }
- func importExpr(moduleName string, pos Pos) *ImportExpr {
- return &ImportExpr{
- ModuleName: moduleName, Token: token.Import, TokenPos: pos,
- }
- }
- func exprs(list ...Expr) []Expr {
- return list
- }
- func intLit(value int64, pos Pos) *IntLit {
- return &IntLit{Value: value, ValuePos: pos}
- }
- func floatLit(value float64, pos Pos) *FloatLit {
- return &FloatLit{Value: value, ValuePos: pos}
- }
- func stringLit(value string, pos Pos) *StringLit {
- return &StringLit{Value: value, ValuePos: pos}
- }
- func charLit(value rune, pos Pos) *CharLit {
- return &CharLit{
- Value: value, ValuePos: pos, Literal: fmt.Sprintf("'%c'", value),
- }
- }
- func boolLit(value bool, pos Pos) *BoolLit {
- return &BoolLit{Value: value, ValuePos: pos}
- }
- func arrayLit(lbracket, rbracket Pos, list ...Expr) *ArrayLit {
- return &ArrayLit{LBrack: lbracket, RBrack: rbracket, Elements: list}
- }
- func mapElementLit(
- key string,
- keyPos Pos,
- colonPos Pos,
- value Expr,
- ) *MapElementLit {
- return &MapElementLit{
- Key: key, KeyPos: keyPos, ColonPos: colonPos, Value: value,
- }
- }
- func mapLit(
- lbrace, rbrace Pos,
- list ...*MapElementLit,
- ) *MapLit {
- return &MapLit{LBrace: lbrace, RBrace: rbrace, Elements: list}
- }
- func funcLit(funcType *FuncType, body *BlockStmt) *FuncLit {
- return &FuncLit{Type: funcType, Body: body}
- }
- func parenExpr(x Expr, lparen, rparen Pos) *ParenExpr {
- return &ParenExpr{Expr: x, LParen: lparen, RParen: rparen}
- }
- func callExpr(
- f Expr,
- lparen, rparen, ellipsis Pos,
- args ...Expr,
- ) *CallExpr {
- return &CallExpr{Func: f, LParen: lparen, RParen: rparen,
- Ellipsis: ellipsis, Args: args}
- }
- func indexExpr(
- x, index Expr,
- lbrack, rbrack Pos,
- ) *IndexExpr {
- return &IndexExpr{
- Expr: x, Index: index, LBrack: lbrack, RBrack: rbrack,
- }
- }
- func sliceExpr(
- x, low, high Expr,
- lbrack, rbrack Pos,
- ) *SliceExpr {
- return &SliceExpr{
- Expr: x, Low: low, High: high, LBrack: lbrack, RBrack: rbrack,
- }
- }
- func errorExpr(
- pos Pos,
- x Expr,
- lparen, rparen Pos,
- ) *ErrorExpr {
- return &ErrorExpr{
- Expr: x, ErrorPos: pos, LParen: lparen, RParen: rparen,
- }
- }
- func selectorExpr(x, sel Expr) *SelectorExpr {
- return &SelectorExpr{Expr: x, Sel: sel}
- }
- func equalStmt(t *testing.T, expected, actual Stmt) {
- if expected == nil || reflect.ValueOf(expected).IsNil() {
- require.Nil(t, actual, "expected nil, but got not nil")
- return
- }
- require.NotNil(t, actual, "expected not nil, but got nil")
- require.IsType(t, expected, actual)
- switch expected := expected.(type) {
- case *ExprStmt:
- equalExpr(t, expected.Expr, actual.(*ExprStmt).Expr)
- case *EmptyStmt:
- require.Equal(t, expected.Implicit,
- actual.(*EmptyStmt).Implicit)
- require.Equal(t, expected.Semicolon,
- actual.(*EmptyStmt).Semicolon)
- case *BlockStmt:
- require.Equal(t, expected.LBrace,
- actual.(*BlockStmt).LBrace)
- require.Equal(t, expected.RBrace,
- actual.(*BlockStmt).RBrace)
- equalStmts(t, expected.Stmts,
- actual.(*BlockStmt).Stmts)
- case *AssignStmt:
- equalExprs(t, expected.LHS,
- actual.(*AssignStmt).LHS)
- equalExprs(t, expected.RHS,
- actual.(*AssignStmt).RHS)
- require.Equal(t, int(expected.Token),
- int(actual.(*AssignStmt).Token))
- require.Equal(t, int(expected.TokenPos),
- int(actual.(*AssignStmt).TokenPos))
- case *IfStmt:
- equalStmt(t, expected.Init, actual.(*IfStmt).Init)
- equalExpr(t, expected.Cond, actual.(*IfStmt).Cond)
- equalStmt(t, expected.Body, actual.(*IfStmt).Body)
- equalStmt(t, expected.Else, actual.(*IfStmt).Else)
- require.Equal(t, expected.IfPos, actual.(*IfStmt).IfPos)
- case *IncDecStmt:
- equalExpr(t, expected.Expr,
- actual.(*IncDecStmt).Expr)
- require.Equal(t, expected.Token,
- actual.(*IncDecStmt).Token)
- require.Equal(t, expected.TokenPos,
- actual.(*IncDecStmt).TokenPos)
- case *ForStmt:
- equalStmt(t, expected.Init, actual.(*ForStmt).Init)
- equalExpr(t, expected.Cond, actual.(*ForStmt).Cond)
- equalStmt(t, expected.Post, actual.(*ForStmt).Post)
- equalStmt(t, expected.Body, actual.(*ForStmt).Body)
- require.Equal(t, expected.ForPos, actual.(*ForStmt).ForPos)
- case *ForInStmt:
- equalExpr(t, expected.Key,
- actual.(*ForInStmt).Key)
- equalExpr(t, expected.Value,
- actual.(*ForInStmt).Value)
- equalExpr(t, expected.Iterable,
- actual.(*ForInStmt).Iterable)
- equalStmt(t, expected.Body,
- actual.(*ForInStmt).Body)
- require.Equal(t, expected.ForPos,
- actual.(*ForInStmt).ForPos)
- case *ReturnStmt:
- equalExpr(t, expected.Result,
- actual.(*ReturnStmt).Result)
- require.Equal(t, expected.ReturnPos,
- actual.(*ReturnStmt).ReturnPos)
- case *BranchStmt:
- equalExpr(t, expected.Label,
- actual.(*BranchStmt).Label)
- require.Equal(t, expected.Token,
- actual.(*BranchStmt).Token)
- require.Equal(t, expected.TokenPos,
- actual.(*BranchStmt).TokenPos)
- default:
- panic(fmt.Errorf("unknown type: %T", expected))
- }
- }
- func equalExpr(t *testing.T, expected, actual Expr) {
- if expected == nil || reflect.ValueOf(expected).IsNil() {
- require.Nil(t, actual, "expected nil, but got not nil")
- return
- }
- require.NotNil(t, actual, "expected not nil, but got nil")
- require.IsType(t, expected, actual)
- switch expected := expected.(type) {
- case *Ident:
- require.Equal(t, expected.Name,
- actual.(*Ident).Name)
- require.Equal(t, int(expected.NamePos),
- int(actual.(*Ident).NamePos))
- case *IntLit:
- require.Equal(t, expected.Value,
- actual.(*IntLit).Value)
- require.Equal(t, int(expected.ValuePos),
- int(actual.(*IntLit).ValuePos))
- case *FloatLit:
- require.Equal(t, expected.Value,
- actual.(*FloatLit).Value)
- require.Equal(t, int(expected.ValuePos),
- int(actual.(*FloatLit).ValuePos))
- case *BoolLit:
- require.Equal(t, expected.Value,
- actual.(*BoolLit).Value)
- require.Equal(t, int(expected.ValuePos),
- int(actual.(*BoolLit).ValuePos))
- case *CharLit:
- require.Equal(t, expected.Value,
- actual.(*CharLit).Value)
- require.Equal(t, int(expected.ValuePos),
- int(actual.(*CharLit).ValuePos))
- case *StringLit:
- require.Equal(t, expected.Value,
- actual.(*StringLit).Value)
- require.Equal(t, int(expected.ValuePos),
- int(actual.(*StringLit).ValuePos))
- case *ArrayLit:
- require.Equal(t, expected.LBrack,
- actual.(*ArrayLit).LBrack)
- require.Equal(t, expected.RBrack,
- actual.(*ArrayLit).RBrack)
- equalExprs(t, expected.Elements,
- actual.(*ArrayLit).Elements)
- case *MapLit:
- require.Equal(t, expected.LBrace,
- actual.(*MapLit).LBrace)
- require.Equal(t, expected.RBrace,
- actual.(*MapLit).RBrace)
- equalMapElements(t, expected.Elements,
- actual.(*MapLit).Elements)
- case *BinaryExpr:
- equalExpr(t, expected.LHS,
- actual.(*BinaryExpr).LHS)
- equalExpr(t, expected.RHS,
- actual.(*BinaryExpr).RHS)
- require.Equal(t, expected.Token,
- actual.(*BinaryExpr).Token)
- require.Equal(t, expected.TokenPos,
- actual.(*BinaryExpr).TokenPos)
- case *UnaryExpr:
- equalExpr(t, expected.Expr,
- actual.(*UnaryExpr).Expr)
- require.Equal(t, expected.Token,
- actual.(*UnaryExpr).Token)
- require.Equal(t, expected.TokenPos,
- actual.(*UnaryExpr).TokenPos)
- case *FuncLit:
- equalFuncType(t, expected.Type,
- actual.(*FuncLit).Type)
- equalStmt(t, expected.Body,
- actual.(*FuncLit).Body)
- case *CallExpr:
- equalExpr(t, expected.Func,
- actual.(*CallExpr).Func)
- require.Equal(t, expected.LParen,
- actual.(*CallExpr).LParen)
- require.Equal(t, expected.RParen,
- actual.(*CallExpr).RParen)
- equalExprs(t, expected.Args,
- actual.(*CallExpr).Args)
- case *ParenExpr:
- equalExpr(t, expected.Expr,
- actual.(*ParenExpr).Expr)
- require.Equal(t, expected.LParen,
- actual.(*ParenExpr).LParen)
- require.Equal(t, expected.RParen,
- actual.(*ParenExpr).RParen)
- case *IndexExpr:
- equalExpr(t, expected.Expr,
- actual.(*IndexExpr).Expr)
- equalExpr(t, expected.Index,
- actual.(*IndexExpr).Index)
- require.Equal(t, expected.LBrack,
- actual.(*IndexExpr).LBrack)
- require.Equal(t, expected.RBrack,
- actual.(*IndexExpr).RBrack)
- case *SliceExpr:
- equalExpr(t, expected.Expr,
- actual.(*SliceExpr).Expr)
- equalExpr(t, expected.Low,
- actual.(*SliceExpr).Low)
- equalExpr(t, expected.High,
- actual.(*SliceExpr).High)
- require.Equal(t, expected.LBrack,
- actual.(*SliceExpr).LBrack)
- require.Equal(t, expected.RBrack,
- actual.(*SliceExpr).RBrack)
- case *SelectorExpr:
- equalExpr(t, expected.Expr,
- actual.(*SelectorExpr).Expr)
- equalExpr(t, expected.Sel,
- actual.(*SelectorExpr).Sel)
- case *ImportExpr:
- require.Equal(t, expected.ModuleName,
- actual.(*ImportExpr).ModuleName)
- require.Equal(t, int(expected.TokenPos),
- int(actual.(*ImportExpr).TokenPos))
- require.Equal(t, expected.Token,
- actual.(*ImportExpr).Token)
- case *ErrorExpr:
- equalExpr(t, expected.Expr,
- actual.(*ErrorExpr).Expr)
- require.Equal(t, int(expected.ErrorPos),
- int(actual.(*ErrorExpr).ErrorPos))
- require.Equal(t, int(expected.LParen),
- int(actual.(*ErrorExpr).LParen))
- require.Equal(t, int(expected.RParen),
- int(actual.(*ErrorExpr).RParen))
- case *CondExpr:
- equalExpr(t, expected.Cond,
- actual.(*CondExpr).Cond)
- equalExpr(t, expected.True,
- actual.(*CondExpr).True)
- equalExpr(t, expected.False,
- actual.(*CondExpr).False)
- require.Equal(t, expected.QuestionPos,
- actual.(*CondExpr).QuestionPos)
- require.Equal(t, expected.ColonPos,
- actual.(*CondExpr).ColonPos)
- default:
- panic(fmt.Errorf("unknown type: %T", expected))
- }
- }
- func equalFuncType(t *testing.T, expected, actual *FuncType) {
- require.Equal(t, expected.Params.LParen, actual.Params.LParen)
- require.Equal(t, expected.Params.RParen, actual.Params.RParen)
- equalIdents(t, expected.Params.List, actual.Params.List)
- }
- func equalIdents(t *testing.T, expected, actual []*Ident) {
- require.Equal(t, len(expected), len(actual))
- for i := 0; i < len(expected); i++ {
- equalExpr(t, expected[i], actual[i])
- }
- }
- func equalExprs(t *testing.T, expected, actual []Expr) {
- require.Equal(t, len(expected), len(actual))
- for i := 0; i < len(expected); i++ {
- equalExpr(t, expected[i], actual[i])
- }
- }
- func equalStmts(t *testing.T, expected, actual []Stmt) {
- require.Equal(t, len(expected), len(actual))
- for i := 0; i < len(expected); i++ {
- equalStmt(t, expected[i], actual[i])
- }
- }
- func equalMapElements(
- t *testing.T,
- expected, actual []*MapElementLit,
- ) {
- require.Equal(t, len(expected), len(actual))
- for i := 0; i < len(expected); i++ {
- require.Equal(t, expected[i].Key, actual[i].Key)
- require.Equal(t, expected[i].KeyPos, actual[i].KeyPos)
- require.Equal(t, expected[i].ColonPos, actual[i].ColonPos)
- equalExpr(t, expected[i].Value, actual[i].Value)
- }
- }
- func parseSource(
- filename string,
- src []byte,
- trace io.Writer,
- ) (res *File, err error) {
- fileSet := NewFileSet()
- file := fileSet.AddFile(filename, -1, len(src))
- p := NewParser(file, src, trace)
- return p.ParseFile()
- }
|