@@ -1456,7 +1456,7 @@ function tokenizeFile(path, keepWhitespaceTokens)
14561456 assertArg2 (" tokenizeFile" , 2 , keepWhitespaceTokens , " boolean" ," nil" )
14571457
14581458 local file , err = ioOpen (path , " r" )
1459- if not file then return nil , err end
1459+ if not file then return nil , F ( " Could not open file '%s'. (%s) " , ensurePrintable ( path ), ensurePrintable ( err )) end
14601460
14611461 local s = file :read (" *a" )
14621462 file :close ()
@@ -4958,6 +4958,11 @@ do
49584958 end
49594959 end
49604960
4961+ local function choosePretty (node , prettyFallback )
4962+ if node .pretty ~= nil then return node .pretty end
4963+ return prettyFallback
4964+ end
4965+
49614966 local function writeLua (buffer , lua , lastOutput )
49624967 tableInsert (buffer , lua )
49634968 return lastOutput
50275032
50285033 lastOutput = writeLua (buffer , " )" , " " )
50295034 if nodeCb then nodeCb (func .body , buffer ) end
5035+ pretty = choosePretty (func .body , pretty )
50305036 if pretty then lastOutput = writeLua (buffer , " \n " , " " ) end
50315037
50325038 local ok ;ok , lastOutput = writeStatements (buffer , pretty , indent + 1 , lastOutput , func .body .statements , nodeCb )
50545060 local decl = statement
50555061 local assignment = statementNext
50565062 local func = assignment .values [1 ]
5063+ local pretty = choosePretty (assignment , pretty )
50575064
50585065 if nodeCb then
50595066 nodeCb (decl , buffer )
50695076 local ok ;ok , lastOutput = writeNode (buffer , pretty , indent , lastOutput , assignment .targets [1 ], true , nodeCb )
50705077 if not ok then return nil , lastOutput end
50715078
5072- local ok ;ok , lastOutput = writeFunctionParametersAndBody (buffer , pretty , indent , lastOutput , func , func .parameters , nil , nodeCb )
5079+ local ok ;ok , lastOutput = writeFunctionParametersAndBody (buffer , choosePretty ( func , pretty ) , indent , lastOutput , func , func .parameters , nil , nodeCb )
50735080 if not ok then return nil , lastOutput end
50745081
50755082 skipNext = true
@@ -5108,8 +5115,10 @@ do
51085115
51095116 if canNodeBeName (lookup .member ) then
51105117 lastOutput = writeLua (buffer , (forMethodCall and " :" or " ." ), " " )
5111- if nodeCb then nodeCb (lookup .member , buffer ) end
5118+ if nodeCb then nodeCb (lookup .member , buffer ) end
5119+ if lookup .member .prefix then tableInsert (buffer , lookup .member .prefix ) end
51125120 lastOutput = writeAlphanum (buffer , pretty , lookup .member .value , lastOutput )
5121+ if lookup .member .suffix then tableInsert (buffer , lookup .member .suffix ) end
51135122
51145123 elseif forMethodCall then
51155124 return nil , " Error: AST: Callee for method call is not a lookup."
51495158
51505159 if l .type == " binary" and l .operator == binary .operator then
51515160 if nodeCb then nodeCb (l , buffer ) end
5152- local ok ;ok , lastOutput = writeBinaryOperatorChain (buffer , pretty , indent , lastOutput , l , nodeCb )
5161+ local ok ;ok , lastOutput = writeBinaryOperatorChain (buffer , choosePretty ( l , pretty ) , indent , lastOutput , l , nodeCb )
51535162 if not ok then return nil , lastOutput end
51545163 else
51555164 local ok ;ok , lastOutput = writeNode (buffer , pretty , indent , lastOutput , l , false , nodeCb )
51735182
51745183 if r .type == " binary" and r .operator == binary .operator then
51755184 if nodeCb then nodeCb (r , buffer ) end
5176- local ok ;ok , lastOutput = writeBinaryOperatorChain (buffer , pretty , indent , lastOutput , r , nodeCb )
5185+ local ok ;ok , lastOutput = writeBinaryOperatorChain (buffer , choosePretty ( r , pretty ) , indent , lastOutput , r , nodeCb )
51775186 if not ok then return nil , lastOutput end
51785187 else
51795188 local ok ;ok , lastOutput = writeNode (buffer , pretty , indent , lastOutput , r , false , nodeCb )
@@ -5187,8 +5196,12 @@ do
51875196 -- Returns nil and a message or error.
51885197 function writeNode (buffer , pretty , indent , lastOutput , node , maySafelyOmitParens , nodeCb )
51895198 if nodeCb then nodeCb (node , buffer ) end
5199+ pretty = choosePretty (node , pretty ) -- @Doc: AstNode.pretty
5200+
51905201 local nodeType = node .type
51915202
5203+ if node .prefix then tableInsert (buffer , node .prefix ) end -- @Doc: AstNode.prefix @Incomplete: Do this everywhere.
5204+
51925205 -- Expressions:
51935206
51945207 if nodeType == " identifier" then
51975210
51985211 elseif nodeType == " vararg" then
51995212 local vararg = node
5200- if vararg .adjustToOne then lastOutput = writeLua (buffer , " (" , " " ) end
5213+ if vararg .adjustToOne then lastOutput = writeLua (buffer , " (" , " " ) end
52015214 if lastOutput == " ." then lastOutput = writeLua (buffer , " " , " ." ) end
52025215 lastOutput = writeLua (buffer , " ..." , " ." )
52035216 if vararg .adjustToOne then lastOutput = writeLua (buffer , " )" , " " ) end
53795392
53805393 if nodeCb then nodeCb (lookup , buffer ) end
53815394
5382- local ok ;ok , lastOutput = writeLookup (buffer , pretty , indent , lastOutput , lookup , true , nodeCb )
5395+ local ok ;ok , lastOutput = writeLookup (buffer , choosePretty ( lookup , pretty ) , indent , lastOutput , lookup , true , nodeCb )
53835396 if not ok then return nil , lastOutput end
53845397
53855398 else
@@ -5544,21 +5557,23 @@ do
55445557 if pretty then lastOutput = writeLua (buffer , " " , " " ) end
55455558 lastOutput = writeAlphanum (buffer , pretty , " then" , lastOutput )
55465559 if nodeCb then nodeCb (ifNode .bodyTrue , buffer ) end
5547- if pretty then lastOutput = writeLua (buffer , " \n " , " " ) end
5560+ local prettyBody = choosePretty (ifNode .bodyTrue , pretty )
5561+ if prettyBody then lastOutput = writeLua (buffer , " \n " , " " ) end
55485562
5549- local ok ;ok , lastOutput = writeStatements (buffer , pretty , indent + 1 , lastOutput , ifNode .bodyTrue .statements , nodeCb )
5563+ local ok ;ok , lastOutput = writeStatements (buffer , prettyBody , indent + 1 , lastOutput , ifNode .bodyTrue .statements , nodeCb )
55505564 if not ok then return nil , lastOutput end
55515565
55525566 while ifNode .bodyFalse do
5567+ lastOutput = writeIndentationIfPretty (buffer , prettyBody , indent , lastOutput )
5568+
55535569 -- Automatically detect what looks like 'elseif'.
55545570 if # ifNode .bodyFalse .statements == 1 and ifNode .bodyFalse .statements [1 ].type == " if" then
5555- lastOutput = writeIndentationIfPretty (buffer , pretty , indent , lastOutput )
5556-
55575571 if nodeCb then nodeCb (ifNode .bodyFalse , buffer ) end
55585572 ifNode = ifNode .bodyFalse .statements [1 ]
55595573 if nodeCb then nodeCb (ifNode , buffer ) end
5574+ pretty = choosePretty (ifNode , pretty )
55605575
5561- lastOutput = writeAlphanum (buffer , pretty , " elseif" , lastOutput )
5576+ lastOutput = writeAlphanum (buffer , prettyBody , " elseif" , lastOutput )
55625577 if pretty then lastOutput = writeLua (buffer , " " , " " ) end
55635578
55645579 local ok ;ok , lastOutput = writeNode (buffer , pretty , indent , lastOutput , ifNode .condition , true , nodeCb )
@@ -5567,26 +5582,27 @@ do
55675582 if pretty then lastOutput = writeLua (buffer , " " , " " ) end
55685583 lastOutput = writeAlphanum (buffer , pretty , " then" , lastOutput )
55695584 if nodeCb then nodeCb (ifNode .bodyTrue , buffer ) end
5570- if pretty then lastOutput = writeLua (buffer , " \n " , " " ) end
5585+ prettyBody = choosePretty (ifNode .bodyTrue , pretty )
5586+ if prettyBody then lastOutput = writeLua (buffer , " \n " , " " ) end
55715587
5572- local ok ;ok , lastOutput = writeStatements (buffer , pretty , indent + 1 , lastOutput , ifNode .bodyTrue .statements , nodeCb )
5588+ local ok ;ok , lastOutput = writeStatements (buffer , prettyBody , indent + 1 , lastOutput , ifNode .bodyTrue .statements , nodeCb )
55735589 if not ok then return nil , lastOutput end
55745590
55755591 else
5576- lastOutput = writeIndentationIfPretty (buffer , pretty , indent , lastOutput )
5577- lastOutput = writeAlphanum (buffer , pretty , " else" , lastOutput )
5592+ lastOutput = writeAlphanum (buffer , prettyBody , " else" , lastOutput )
55785593 if nodeCb then nodeCb (ifNode .bodyFalse , buffer ) end
5579- if pretty then lastOutput = writeLua (buffer , " \n " , " " ) end
5594+ prettyBody = choosePretty (ifNode .bodyFalse , pretty )
5595+ if prettyBody then lastOutput = writeLua (buffer , " \n " , " " ) end
55805596
5581- local ok ;ok , lastOutput = writeStatements (buffer , pretty , indent + 1 , lastOutput , ifNode .bodyFalse .statements , nodeCb )
5597+ local ok ;ok , lastOutput = writeStatements (buffer , prettyBody , indent + 1 , lastOutput , ifNode .bodyFalse .statements , nodeCb )
55825598 if not ok then return nil , lastOutput end
55835599
55845600 break
55855601 end
55865602 end
55875603
5588- lastOutput = writeIndentationIfPretty (buffer , pretty , indent , lastOutput )
5589- lastOutput = writeAlphanum (buffer , pretty , " end" , lastOutput )
5604+ lastOutput = writeIndentationIfPretty (buffer , prettyBody , indent , lastOutput )
5605+ lastOutput = writeAlphanum (buffer , prettyBody , " end" , lastOutput )
55905606
55915607 elseif nodeType == " while" then
55925608 local whileLoop = node
@@ -5598,26 +5614,29 @@ do
55985614
55995615 if pretty then lastOutput = writeLua (buffer , " " , " " ) end
56005616 lastOutput = writeAlphanum (buffer , pretty , " do" , lastOutput )
5617+
56015618 if nodeCb then nodeCb (whileLoop .body , buffer ) end
5602- if pretty then lastOutput = writeLua (buffer , " \n " , " " ) end
5619+ local prettyBody = choosePretty (whileLoop .body , pretty )
5620+ if prettyBody then lastOutput = writeLua (buffer , " \n " , " " ) end
56035621
5604- local ok ;ok , lastOutput = writeStatements (buffer , pretty , indent + 1 , lastOutput , whileLoop .body .statements , nodeCb )
5622+ local ok ;ok , lastOutput = writeStatements (buffer , prettyBody , indent + 1 , lastOutput , whileLoop .body .statements , nodeCb )
56055623 if not ok then return nil , lastOutput end
56065624
5607- lastOutput = writeIndentationIfPretty (buffer , pretty , indent , lastOutput )
5608- lastOutput = writeAlphanum (buffer , pretty , " end" , lastOutput )
5625+ lastOutput = writeIndentationIfPretty (buffer , prettyBody , indent , lastOutput )
5626+ lastOutput = writeAlphanum (buffer , prettyBody , " end" , lastOutput )
56095627
56105628 elseif nodeType == " repeat" then
56115629 local repeatLoop = node
56125630 lastOutput = writeAlphanum (buffer , pretty , " repeat" , lastOutput )
56135631 if nodeCb then nodeCb (repeatLoop .body , buffer ) end
5614- if pretty then lastOutput = writeLua (buffer , " \n " , " " ) end
5632+ local prettyBody = choosePretty (repeatLoop .body , pretty )
5633+ if prettyBody then lastOutput = writeLua (buffer , " \n " , " " ) end
56155634
5616- local ok ;ok , lastOutput = writeStatements (buffer , pretty , indent + 1 , lastOutput , repeatLoop .body .statements , nodeCb )
5635+ local ok ;ok , lastOutput = writeStatements (buffer , prettyBody , indent + 1 , lastOutput , repeatLoop .body .statements , nodeCb )
56175636 if not ok then return nil , lastOutput end
56185637
5619- lastOutput = writeIndentationIfPretty (buffer , pretty , indent , lastOutput )
5620- lastOutput = writeAlphanum (buffer , pretty , " until" , lastOutput )
5638+ lastOutput = writeIndentationIfPretty (buffer , prettyBody , indent , lastOutput )
5639+ lastOutput = writeAlphanum (buffer , prettyBody , " until" , lastOutput )
56215640 if pretty then lastOutput = writeLua (buffer , " " , " " ) end
56225641
56235642 local ok ;ok , lastOutput = writeNode (buffer , pretty , indent , lastOutput , repeatLoop .condition , true , nodeCb )
@@ -5652,17 +5671,21 @@ do
56525671 if pretty then lastOutput = writeLua (buffer , " " , " " ) end
56535672 lastOutput = writeAlphanum (buffer , pretty , " do" , lastOutput )
56545673 if nodeCb then nodeCb (forLoop .body , buffer ) end
5655- if pretty then lastOutput = writeLua (buffer , " \n " , " " ) end
5674+ local prettyBody = choosePretty (forLoop .body , pretty )
5675+ if prettyBody then lastOutput = writeLua (buffer , " \n " , " " ) end
56565676
5657- local ok ;ok , lastOutput = writeStatements (buffer , pretty , indent + 1 , lastOutput , forLoop .body .statements , nodeCb )
5677+ local ok ;ok , lastOutput = writeStatements (buffer , prettyBody , indent + 1 , lastOutput , forLoop .body .statements , nodeCb )
56585678 if not ok then return nil , lastOutput end
56595679
5660- lastOutput = writeIndentationIfPretty (buffer , pretty , indent , lastOutput )
5661- lastOutput = writeAlphanum (buffer , pretty , " end" , lastOutput )
5680+ lastOutput = writeIndentationIfPretty (buffer , prettyBody , indent , lastOutput )
5681+ lastOutput = writeAlphanum (buffer , prettyBody , " end" , lastOutput )
56625682
56635683 else
56645684 return false , F (" Error: Unknown node type '%s'." , tostring (nodeType ))
56655685 end
5686+
5687+ if node .suffix then tableInsert (buffer , node .suffix ) end -- @Doc: AstNode.suffix @Incomplete: Do this everywhere.
5688+
56665689 return true , lastOutput
56675690 end
56685691
56775700 local ok , err
56785701 if node .type == " block" then -- @Robustness: This exception isn't great. Should there be a file scope node?
56795702 if nodeCb then nodeCb (node , buffer ) end
5680- ok , err = writeStatements (buffer , pretty , 0 , " " , node .statements , nodeCb )
5703+ ok , err = writeStatements (buffer , choosePretty ( node , pretty ) , 0 , " " , node .statements , nodeCb )
56815704 else
56825705 ok , err = writeNode (buffer , pretty , 0 , " " , node , true , nodeCb )
56835706 end
62716294 -- @Incomplete: Should we detect nil literal keys? :DetectRuntimeErrors
62726295 if not tableField .key then
62736296 if not tableField .generatedKey then
6274- addValidationError (path , errors , " Missing 'key' field for table field %d." , i )
6297+ addValidationError (path , errors , " Missing 'key' field for table field %d." , i )
62756298 end
62766299 elseif not EXPRESSION_NODES [tableField .key .type ] then
62776300 addValidationError (path , errors , " The key for table field %d is not an expression. (It is '%s'.)" , i , tableField .key .type )
0 commit comments