parser.lua 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 objectfile = target:objectfile(sourcefile_cxx)
  43. table.insert(target:objectfiles(), objectfile)
  44. batchcmds:show_progress(opt.progress, "${color.build.object}compiling.$(mode) %s", sourcefile_cxx)
  45. batchcmds:compile(sourcefile_cxx, objectfile)
  46. batchcmds:add_depfiles(sourcefile_g4)
  47. batchcmds:set_depmtime(os.mtime(objectfile))
  48. batchcmds:set_depcache(target:dependfile(objectfile))
  49. end)