prettyprinter.go 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /*
  2. * ECAL
  3. *
  4. * Copyright 2020 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the MIT
  7. * License, If a copy of the MIT License was not distributed with this
  8. * file, You can obtain one at https://opensource.org/licenses/MIT.
  9. */
  10. package parser
  11. import (
  12. "bufio"
  13. "bytes"
  14. "fmt"
  15. "strconv"
  16. "strings"
  17. "text/template"
  18. "unicode"
  19. "devt.de/krotik/common/errorutil"
  20. "devt.de/krotik/common/stringutil"
  21. )
  22. /*
  23. IndentationLevel is the level of indentation which the pretty printer should use
  24. */
  25. const IndentationLevel = 4
  26. /*
  27. Map of AST nodes corresponding to lexer tokens
  28. */
  29. var prettyPrinterMap map[string]*template.Template
  30. /*
  31. Map of nodes where the precedence might have changed because of parentheses
  32. */
  33. var bracketPrecedenceMap map[string]bool
  34. func init() {
  35. prettyPrinterMap = map[string]*template.Template{
  36. NodeSTRING: template.Must(template.New(NodeSTRING).Parse("{{.qval}}")),
  37. NodeNUMBER: template.Must(template.New(NodeNUMBER).Parse("{{.val}}")),
  38. // NodeIDENTIFIER - Special case (handled in code)
  39. // Constructed tokens
  40. // NodeSTATEMENTS - Special case (handled in code)
  41. // NodeFUNCCALL - Special case (handled in code)
  42. NodeCOMPACCESS + "_1": template.Must(template.New(NodeCOMPACCESS).Parse("[{{.c1}}]")),
  43. // TokenLIST - Special case (handled in code)
  44. // TokenMAP - Special case (handled in code)
  45. // TokenPARAMS - Special case (handled in code)
  46. NodeGUARD + "_1": template.Must(template.New(NodeGUARD).Parse("{{.c1}}")),
  47. // Condition operators
  48. NodeGEQ + "_2": template.Must(template.New(NodeGEQ).Parse("{{.c1}} >= {{.c2}}")),
  49. NodeLEQ + "_2": template.Must(template.New(NodeLEQ).Parse("{{.c1}} <= {{.c2}}")),
  50. NodeNEQ + "_2": template.Must(template.New(NodeNEQ).Parse("{{.c1}} != {{.c2}}")),
  51. NodeEQ + "_2": template.Must(template.New(NodeEQ).Parse("{{.c1}} == {{.c2}}")),
  52. NodeGT + "_2": template.Must(template.New(NodeGT).Parse("{{.c1}} > {{.c2}}")),
  53. NodeLT + "_2": template.Must(template.New(NodeLT).Parse("{{.c1}} < {{.c2}}")),
  54. // Separators
  55. NodeKVP + "_2": template.Must(template.New(NodeKVP).Parse("{{.c1}} : {{.c2}}")),
  56. NodePRESET + "_2": template.Must(template.New(NodePRESET).Parse("{{.c1}}={{.c2}}")),
  57. // Arithmetic operators
  58. NodePLUS + "_1": template.Must(template.New(NodePLUS).Parse("+{{.c1}}")),
  59. NodePLUS + "_2": template.Must(template.New(NodePLUS).Parse("{{.c1}} + {{.c2}}")),
  60. NodeMINUS + "_1": template.Must(template.New(NodeMINUS).Parse("-{{.c1}}")),
  61. NodeMINUS + "_2": template.Must(template.New(NodeMINUS).Parse("{{.c1}} - {{.c2}}")),
  62. NodeTIMES + "_2": template.Must(template.New(NodeTIMES).Parse("{{.c1}} * {{.c2}}")),
  63. NodeDIV + "_2": template.Must(template.New(NodeDIV).Parse("{{.c1}} / {{.c2}}")),
  64. NodeMODINT + "_2": template.Must(template.New(NodeMODINT).Parse("{{.c1}} % {{.c2}}")),
  65. NodeDIVINT + "_2": template.Must(template.New(NodeDIVINT).Parse("{{.c1}} // {{.c2}}")),
  66. // Assignment statement
  67. NodeASSIGN + "_2": template.Must(template.New(NodeASSIGN).Parse("{{.c1}} := {{.c2}}")),
  68. NodeLET + "_1": template.Must(template.New(NodeASSIGN).Parse("let {{.c1}}")),
  69. // Import statement
  70. NodeIMPORT + "_2": template.Must(template.New(NodeIMPORT).Parse("import {{.c1}} as {{.c2}}")),
  71. NodeAS + "_1": template.Must(template.New(NodeRETURN).Parse("as {{.c1}}")),
  72. // Sink definition
  73. // NodeSINK - Special case (handled in code)
  74. NodeKINDMATCH + "_1": template.Must(template.New(NodeKINDMATCH).Parse("kindmatch {{.c1}}")),
  75. NodeSCOPEMATCH + "_1": template.Must(template.New(NodeSCOPEMATCH).Parse("scopematch {{.c1}}")),
  76. NodeSTATEMATCH + "_1": template.Must(template.New(NodeSTATEMATCH).Parse("statematch {{.c1}}")),
  77. NodePRIORITY + "_1": template.Must(template.New(NodePRIORITY).Parse("priority {{.c1}}")),
  78. NodeSUPPRESSES + "_1": template.Must(template.New(NodeSUPPRESSES).Parse("suppresses {{.c1}}")),
  79. // Function definition
  80. NodeFUNC + "_2": template.Must(template.New(NodeFUNC).Parse("func {{.c1}} {\n{{.c2}}}")),
  81. NodeFUNC + "_3": template.Must(template.New(NodeFUNC).Parse("func {{.c1}}{{.c2}} {\n{{.c3}}}")),
  82. NodeRETURN: template.Must(template.New(NodeRETURN).Parse("return")),
  83. NodeRETURN + "_1": template.Must(template.New(NodeRETURN).Parse("return {{.c1}}")),
  84. // Boolean operators
  85. NodeOR + "_2": template.Must(template.New(NodeOR).Parse("{{.c1}} or {{.c2}}")),
  86. NodeAND + "_2": template.Must(template.New(NodeAND).Parse("{{.c1}} and {{.c2}}")),
  87. NodeNOT + "_1": template.Must(template.New(NodeNOT).Parse("not {{.c1}}")),
  88. // Condition operators
  89. NodeLIKE + "_2": template.Must(template.New(NodeLIKE).Parse("{{.c1}} like {{.c2}}")),
  90. NodeIN + "_2": template.Must(template.New(NodeIN).Parse("{{.c1}} in {{.c2}}")),
  91. NodeHASPREFIX + "_2": template.Must(template.New(NodeHASPREFIX).Parse("{{.c1}} hasprefix {{.c2}}")),
  92. NodeHASSUFFIX + "_2": template.Must(template.New(NodeHASSUFFIX).Parse("{{.c1}} hassuffix {{.c2}}")),
  93. NodeNOTIN + "_2": template.Must(template.New(NodeNOTIN).Parse("{{.c1}} notin {{.c2}}")),
  94. // Constant terminals
  95. NodeTRUE: template.Must(template.New(NodeTRUE).Parse("true")),
  96. NodeFALSE: template.Must(template.New(NodeFALSE).Parse("false")),
  97. NodeNULL: template.Must(template.New(NodeNULL).Parse("null")),
  98. // Conditional statements
  99. // TokenIF - Special case (handled in code)
  100. // TokenELIF - Special case (handled in code)
  101. // TokenELSE - Special case (handled in code)
  102. // Loop statement
  103. NodeLOOP + "_2": template.Must(template.New(NodeLOOP).Parse("for {{.c1}} {\n{{.c2}}}")),
  104. NodeBREAK: template.Must(template.New(NodeBREAK).Parse("break")),
  105. NodeCONTINUE: template.Must(template.New(NodeCONTINUE).Parse("continue")),
  106. // Try statement
  107. // TokenTRY - Special case (handled in code)
  108. // TokenEXCEPT - Special case (handled in code)
  109. NodeOTHERWISE + "_1": template.Must(template.New(NodeOTHERWISE).Parse(" otherwise {\n{{.c1}}}")),
  110. NodeFINALLY + "_1": template.Must(template.New(NodeFINALLY).Parse(" finally {\n{{.c1}}}")),
  111. // Mutex block
  112. NodeMUTEX + "_2": template.Must(template.New(NodeLOOP).Parse("mutex {{.c1}} {\n{{.c2}}}\n")),
  113. }
  114. bracketPrecedenceMap = map[string]bool{
  115. NodePLUS: true,
  116. NodeMINUS: true,
  117. NodeAND: true,
  118. NodeOR: true,
  119. }
  120. }
  121. /*
  122. PrettyPrint produces pretty printed code from a given AST.
  123. */
  124. func PrettyPrint(ast *ASTNode) (string, error) {
  125. var visit func(ast *ASTNode, path []*ASTNode) (string, error)
  126. visit = func(ast *ASTNode, path []*ASTNode) (string, error) {
  127. var buf bytes.Buffer
  128. if ast == nil {
  129. return "", fmt.Errorf("Nil pointer in AST")
  130. }
  131. numChildren := len(ast.Children)
  132. tempKey := ast.Name
  133. tempParam := make(map[string]string)
  134. // First pretty print children
  135. if numChildren > 0 {
  136. for i, child := range ast.Children {
  137. res, err := visit(child, append(path, child))
  138. if err != nil {
  139. return "", err
  140. }
  141. if _, ok := bracketPrecedenceMap[child.Name]; ok && ast.binding > child.binding {
  142. // Put the expression in brackets iff (if and only if) the binding would
  143. // normally order things differently
  144. res = fmt.Sprintf("(%v)", res)
  145. }
  146. tempParam[fmt.Sprint("c", i+1)] = res
  147. }
  148. tempKey += fmt.Sprint("_", len(tempParam))
  149. }
  150. if res, ok := ppSpecialDefs(ast, path, tempParam, &buf); ok {
  151. return res, nil
  152. } else if res, ok := ppSpecialBlocks(ast, path, tempParam, &buf); ok {
  153. return res, nil
  154. } else if res, ok := ppContainerBlocks(ast, path, tempParam, &buf); ok {
  155. return res, nil
  156. } else if res, ok := ppSpecialStatements(ast, path, tempParam, &buf); ok {
  157. return res, nil
  158. }
  159. if ast.Token != nil {
  160. // Adding node value to template parameters
  161. tempParam["val"] = ast.Token.Val
  162. tempParam["qval"] = strconv.Quote(ast.Token.Val)
  163. }
  164. // Retrieve the template
  165. temp, ok := prettyPrinterMap[tempKey]
  166. errorutil.AssertTrue(ok,
  167. fmt.Sprintf("Could not find template for %v (tempkey: %v)",
  168. ast.Name, tempKey))
  169. // Use the children as parameters for template
  170. errorutil.AssertOk(temp.Execute(&buf, tempParam))
  171. return ppPostProcessing(ast, path, buf.String()), nil
  172. }
  173. res, err := visit(ast, []*ASTNode{ast})
  174. return strings.TrimSpace(res), err
  175. }
  176. /*
  177. ppPostProcessing applies post processing rules.
  178. */
  179. func ppPostProcessing(ast *ASTNode, path []*ASTNode, ppString string) string {
  180. // Add meta data
  181. ret := ppMetaData(ast, path, ppString)
  182. // Apply indentation
  183. if len(path) > 1 {
  184. if stringutil.IndexOf(ast.Name, []string{
  185. NodeSTATEMENTS,
  186. NodeMAP,
  187. NodeLIST,
  188. NodeKINDMATCH,
  189. NodeSTATEMATCH,
  190. NodeSCOPEMATCH,
  191. NodePRIORITY,
  192. NodeSUPPRESSES,
  193. }) != -1 {
  194. parent := path[len(path)-2]
  195. indentSpaces := stringutil.GenerateRollingString(" ", IndentationLevel)
  196. ret = strings.ReplaceAll(ret, "\n", "\n"+indentSpaces)
  197. // Add initial indent only if we are inside a block statement
  198. if stringutil.IndexOf(parent.Name, []string{
  199. NodeASSIGN,
  200. NodePRESET,
  201. NodeKVP,
  202. NodeLIST,
  203. NodeFUNCCALL,
  204. NodeKINDMATCH,
  205. NodeSTATEMATCH,
  206. NodeSCOPEMATCH,
  207. NodePRIORITY,
  208. NodeSUPPRESSES,
  209. }) == -1 {
  210. ret = fmt.Sprintf("%v%v", indentSpaces, ret)
  211. }
  212. // Remove indentation from last line unless we have a special case
  213. if stringutil.IndexOf(parent.Name, []string{
  214. NodeSINK,
  215. }) == -1 || ast.Name == NodeSTATEMENTS {
  216. if idx := strings.LastIndex(ret, "\n"); idx != -1 {
  217. ret = ret[:idx+1] + ret[idx+IndentationLevel+1:]
  218. }
  219. }
  220. }
  221. }
  222. if ast.Token != nil {
  223. // Calculate number of extra newlines which should be prefixed the default
  224. // pretty printer assumes a single one which produces very compact code
  225. // the previous formatting might give a hint where to add extra newlines.
  226. // The pretty printer only ever adds a maximum of 1 additional line per
  227. // statement
  228. if ast.Token.PrefixNewlines-1 > 0 {
  229. ret = fmt.Sprintf("\n%v", ret)
  230. }
  231. }
  232. // Remove all trailing spaces
  233. newlineSplit := strings.Split(ret, "\n")
  234. for i, s := range newlineSplit {
  235. newlineSplit[i] = strings.TrimRightFunc(s, unicode.IsSpace)
  236. }
  237. return strings.Join(newlineSplit, "\n")
  238. }
  239. /*
  240. ppMetaData pretty prints comments.
  241. */
  242. func ppMetaData(ast *ASTNode, path []*ASTNode, ppString string) string {
  243. ret := ppString
  244. if len(ast.Meta) > 0 {
  245. for _, meta := range ast.Meta {
  246. metaValue := meta.Value()
  247. if meta.Type() == MetaDataPreComment {
  248. var buf bytes.Buffer
  249. scanner := bufio.NewScanner(strings.NewReader(metaValue))
  250. for scanner.Scan() {
  251. buf.WriteString(fmt.Sprintf(" %v\n", strings.TrimSpace(scanner.Text())))
  252. }
  253. if ast.Token.Lpos != 1 || strings.Index(metaValue, "\n") == -1 {
  254. // Remove the last newline if we are not on the root level
  255. // or we didn't have any newlines in the original comment
  256. buf.Truncate(buf.Len() - 1)
  257. }
  258. if strings.Index(buf.String(), "\n") == -1 {
  259. // If the pretty printed comment does not have any newlines
  260. // Add at least a space at the end
  261. buf.WriteString(" ")
  262. }
  263. ret = fmt.Sprintf("/*%v*/\n%v", buf.String(), ret)
  264. if ast.Token.Lline > 1 {
  265. ret = fmt.Sprintf("\n%v", ret)
  266. }
  267. } else if meta.Type() == MetaDataPostComment {
  268. metaValue = strings.TrimSpace(strings.ReplaceAll(metaValue, "\n", ""))
  269. ret = fmt.Sprintf("%v # %v", ret, metaValue)
  270. }
  271. }
  272. }
  273. return ret
  274. }
  275. /*
  276. ppSpecialDefs pretty prints special cases.
  277. */
  278. func ppSpecialDefs(ast *ASTNode, path []*ASTNode, tempParam map[string]string, buf *bytes.Buffer) (string, bool) {
  279. numChildren := len(ast.Children)
  280. if ast.Name == NodeFUNCCALL {
  281. for i := 0; i < numChildren; i++ {
  282. buf.WriteString(tempParam[fmt.Sprint("c", i+1)])
  283. if i < numChildren-1 {
  284. buf.WriteString(", ")
  285. }
  286. }
  287. return ppPostProcessing(ast, path, buf.String()), true
  288. } else if ast.Name == NodeSINK {
  289. buf.WriteString("sink ")
  290. buf.WriteString(tempParam["c1"])
  291. buf.WriteString("\n")
  292. for i := 1; i < len(ast.Children)-1; i++ {
  293. buf.WriteString(tempParam[fmt.Sprint("c", i+1)])
  294. buf.WriteString("\n")
  295. }
  296. buf.WriteString("{\n")
  297. buf.WriteString(tempParam[fmt.Sprint("c", len(ast.Children))])
  298. buf.WriteString("}\n")
  299. return ppPostProcessing(ast, path, buf.String()), true
  300. }
  301. return "", false
  302. }
  303. /*
  304. ppContainerBlocks pretty prints container structures.
  305. */
  306. func ppContainerBlocks(ast *ASTNode, path []*ASTNode, tempParam map[string]string, buf *bytes.Buffer) (string, bool) {
  307. numChildren := len(ast.Children)
  308. if ast.Name == NodeLIST {
  309. multilineThreshold := 4
  310. buf.WriteString("[")
  311. if numChildren > multilineThreshold {
  312. buf.WriteString("\n")
  313. }
  314. for i := 0; i < numChildren; i++ {
  315. buf.WriteString(tempParam[fmt.Sprint("c", i+1)])
  316. if i < numChildren-1 {
  317. if numChildren > multilineThreshold {
  318. buf.WriteString(",")
  319. } else {
  320. buf.WriteString(", ")
  321. }
  322. }
  323. if numChildren > multilineThreshold {
  324. buf.WriteString("\n")
  325. }
  326. }
  327. buf.WriteString("]")
  328. return ppPostProcessing(ast, path, buf.String()), true
  329. } else if ast.Name == NodeMAP {
  330. multilineThreshold := 2
  331. buf.WriteString("{")
  332. if numChildren > multilineThreshold {
  333. buf.WriteString("\n")
  334. }
  335. for i := 0; i < numChildren; i++ {
  336. buf.WriteString(tempParam[fmt.Sprint("c", i+1)])
  337. if i < numChildren-1 {
  338. if numChildren > multilineThreshold {
  339. buf.WriteString(",")
  340. } else {
  341. buf.WriteString(", ")
  342. }
  343. }
  344. if numChildren > multilineThreshold {
  345. buf.WriteString("\n")
  346. }
  347. }
  348. buf.WriteString("}")
  349. return ppPostProcessing(ast, path, buf.String()), true
  350. }
  351. return "", false
  352. }
  353. /*
  354. ppSpecialBlocks pretty prints special cases.
  355. */
  356. func ppSpecialBlocks(ast *ASTNode, path []*ASTNode, tempParam map[string]string, buf *bytes.Buffer) (string, bool) {
  357. numChildren := len(ast.Children)
  358. // Handle special cases - children in tempParam have been resolved
  359. if ast.Name == NodeSTATEMENTS {
  360. // For statements just concat all children
  361. for i := 0; i < numChildren; i++ {
  362. buf.WriteString(tempParam[fmt.Sprint("c", i+1)])
  363. buf.WriteString("\n")
  364. }
  365. return ppPostProcessing(ast, path, buf.String()), true
  366. } else if ast.Name == NodeTRY {
  367. buf.WriteString("try {\n")
  368. buf.WriteString(tempParam[fmt.Sprint("c1")])
  369. buf.WriteString("}")
  370. for i := 1; i < len(ast.Children); i++ {
  371. buf.WriteString(tempParam[fmt.Sprint("c", i+1)])
  372. }
  373. return ppPostProcessing(ast, path, buf.String()), true
  374. } else if ast.Name == NodeEXCEPT {
  375. buf.WriteString(" except ")
  376. for i := 0; i < len(ast.Children)-1; i++ {
  377. buf.WriteString(tempParam[fmt.Sprint("c", i+1)])
  378. if ast.Children[i+1].Name != NodeAS && i < len(ast.Children)-2 {
  379. buf.WriteString(",")
  380. }
  381. buf.WriteString(" ")
  382. }
  383. buf.WriteString("{\n")
  384. buf.WriteString(tempParam[fmt.Sprint("c", len(ast.Children))])
  385. buf.WriteString("}")
  386. return ppPostProcessing(ast, path, buf.String()), true
  387. }
  388. return "", false
  389. }
  390. /*
  391. ppSpecialStatements pretty prints special cases.
  392. */
  393. func ppSpecialStatements(ast *ASTNode, path []*ASTNode, tempParam map[string]string, buf *bytes.Buffer) (string, bool) {
  394. numChildren := len(ast.Children)
  395. if ast.Name == NodeIDENTIFIER {
  396. buf.WriteString(ast.Token.Val)
  397. for i := 0; i < numChildren; i++ {
  398. if ast.Children[i].Name == NodeIDENTIFIER {
  399. buf.WriteString(".")
  400. buf.WriteString(tempParam[fmt.Sprint("c", i+1)])
  401. } else if ast.Children[i].Name == NodeFUNCCALL {
  402. buf.WriteString("(")
  403. buf.WriteString(tempParam[fmt.Sprint("c", i+1)])
  404. buf.WriteString(")")
  405. } else if ast.Children[i].Name == NodeCOMPACCESS {
  406. buf.WriteString(tempParam[fmt.Sprint("c", i+1)])
  407. }
  408. }
  409. return ppPostProcessing(ast, path, buf.String()), true
  410. } else if ast.Name == NodePARAMS {
  411. buf.WriteString("(")
  412. i := 1
  413. for ; i < numChildren; i++ {
  414. buf.WriteString(tempParam[fmt.Sprint("c", i)])
  415. buf.WriteString(", ")
  416. }
  417. buf.WriteString(tempParam[fmt.Sprint("c", i)])
  418. buf.WriteString(")")
  419. return ppPostProcessing(ast, path, buf.String()), true
  420. } else if ast.Name == NodeIF {
  421. writeGUARD := func(child int) {
  422. buf.WriteString(tempParam[fmt.Sprint("c", child)])
  423. buf.WriteString(" {\n")
  424. buf.WriteString(tempParam[fmt.Sprint("c", child+1)])
  425. buf.WriteString("}")
  426. }
  427. buf.WriteString("if ")
  428. writeGUARD(1)
  429. for i := 0; i < len(ast.Children); i += 2 {
  430. if i+2 == len(ast.Children) && ast.Children[i].Children[0].Name == NodeTRUE {
  431. buf.WriteString(" else {\n")
  432. buf.WriteString(tempParam[fmt.Sprint("c", i+2)])
  433. buf.WriteString("}")
  434. } else if i > 0 {
  435. buf.WriteString(" elif ")
  436. writeGUARD(i + 1)
  437. }
  438. }
  439. return ppPostProcessing(ast, path, buf.String()), true
  440. }
  441. return "", false
  442. }