parser.lua 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. rule("parser")
  2. set_extensions(".g4")
  3. add_deps("@lexer", {order = true})
  4. on_config(function (target)
  5. local includedirs = {}
  6. local autogendir = path.join(target:autogendir(), "rules/antlr4/parser")
  7. for _, sourcebatch in pairs(target:sourcebatches()) do
  8. if sourcebatch.rulename == "@antlr4/parser" then
  9. local sourcefiles = {}
  10. for _, sourcefile in ipairs(sourcebatch.sourcefiles) do
  11. -- remove lexer g4
  12. if not sourcefile:lower():find("lexer", 1, true) then
  13. table.insert(sourcefiles, sourcefile)
  14. table.insert(includedirs, path.normalize(path.join(autogendir, path.directory(sourcefile))))
  15. end
  16. end
  17. sourcebatch.sourcefiles = sourcefiles
  18. break
  19. end
  20. end
  21. target:add("includedirs", table.unique(includedirs), {public = true})
  22. end)
  23. before_buildcmd_file(function (target, batchcmds, sourcefile_g4, opt)
  24. local java = target:data("antlr4.tool")
  25. local argv = target:data("antlr4.tool.argv")
  26. local visitor = target:extraconf("rules", "@antlr4/parser", "visitor")
  27. local listener = target:extraconf("rules", "@antlr4/parser", "listener")
  28. table.insert(argv, (visitor and "-visitor" or "-no-visitor"))
  29. table.insert(argv, (listener and "-listener" or "-no-listener"))
  30. table.join2(argv, target:values("antlr4.parser.flags"))
  31. local autogendir = path.join(target:autogendir(), "rules/antlr4/parser")
  32. local sourcefile_cxx = path.normalize(path.join(autogendir, path.directory(sourcefile_g4), path.basename(sourcefile_g4) .. ".cpp"))
  33. local sourcefile_dir = path.directory(sourcefile_cxx)
  34. batchcmds:mkdir(sourcefile_dir)
  35. table.insert(argv, "-o")
  36. table.insert(argv, autogendir)
  37. table.insert(argv, "-lib")
  38. table.insert(argv, sourcefile_dir)
  39. table.insert(argv, sourcefile_g4)
  40. batchcmds:show_progress(opt.progress, "${color.build.object}compiling.g4 %s", sourcefile_g4)
  41. batchcmds:vrunv(java.program, argv)
  42. local sourcefiles_cxx = {sourcefile_cxx}
  43. local sourcefile_file_dir = path.join(autogendir, path.directory(sourcefile_g4))
  44. if visitor then
  45. table.insert(sourcefiles_cxx, path.join(sourcefile_file_dir, path.basename(sourcefile_g4) .. "Visitor.cpp"))
  46. table.insert(sourcefiles_cxx, path.join(sourcefile_file_dir, path.basename(sourcefile_g4) .. "BaseVisitor.cpp"))
  47. end
  48. if listener then
  49. table.insert(sourcefiles_cxx, path.join(sourcefile_file_dir, path.basename(sourcefile_g4) .. "Listener.cpp"))
  50. table.insert(sourcefiles_cxx, path.join(sourcefile_file_dir, path.basename(sourcefile_g4) .. "BaseListener.cpp"))
  51. end
  52. for _, cxx in ipairs(sourcefiles_cxx) do
  53. local objectfile = target:objectfile(cxx)
  54. table.insert(target:objectfiles(), objectfile)
  55. batchcmds:show_progress(opt.progress, "${color.build.object}compiling.$(mode) %s", cxx)
  56. batchcmds:compile(cxx, objectfile)
  57. if cxx == sourcefile_cxx then
  58. batchcmds:set_depmtime(os.mtime(objectfile))
  59. batchcmds:set_depcache(target:dependfile(objectfile))
  60. end
  61. end
  62. batchcmds:add_depfiles(sourcefile_g4)
  63. end)