Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "Require"s for importing Message Definitions into WireShark #13

Open
emrainey opened this issue Jul 3, 2023 · 5 comments
Open

Add "Require"s for importing Message Definitions into WireShark #13

emrainey opened this issue Jul 3, 2023 · 5 comments
Assignees
Labels
enhancement New feature or request

Comments

@emrainey
Copy link
Collaborator

emrainey commented Jul 3, 2023

Lua can "import" other lua files (which are in the same folder) by calling require "somefile" which will import somefile.lua.

Using this method we could import the Cyphal Heartbeat and GetInfo defintions (and in the future, others).

@emrainey emrainey self-assigned this Jul 3, 2023
@emrainey emrainey added the enhancement New feature or request label Jul 3, 2023
@emrainey
Copy link
Collaborator Author

Looks like there's way to load an ENBF grammar during the script:

local ebnf = require("lua-ebnf")
local grammarFile = "dsdl.enbf"
local grammarENBF = io.open(grammarfile):read("*a")
-- Generate the grammar from the ENBF
local grammar = enbf.parse(grammarENBF)
-- Generate the parser from the EBNF grammar
local parser = ebnf.generate(grammar)
-- Find all the local .dsdl files and parse them
local dsdl = io.open("434.GetTransportStatistics.0.1.dsdl"):read("*a")

It doesn't crash with a fault when it loads this but I'm not convinced it's running on Mac yet.

@emrainey
Copy link
Collaborator Author

It's definite not working. There's no EBNF parser that is open to use. We may have to go down the LPEG route which is a different parser.

@emrainey
Copy link
Collaborator Author

@emrainey
Copy link
Collaborator Author

emrainey commented Sep 8, 2023

Using a PEG parser in python I played with getting very small grammar working:

statement      = (pragma / comment / assignment / declaration)
assignment     = type ~"\s*" identifier ~"\s*" "=" ~"\s*" expression
declaration    = type ~"\s*" identifier
type           = "int" / "float" / "string" / "bool"
expression     = literal / identifier
literal        = hex / floating_point / integer / string / boolean
integer        = ~"\d+"
floating_point = ~"\d+\.\d+"
hex            = ~"0x[0-9a-fA-F]{1,16}"
string         = ~'"(?:[^"]|\\")*"'
boolean        = "true" / "false"
identifier     = ~"[a-zA-Z][a-zA-Z0-9_]*"
comment        = ~"#.*"
keyword        = "sealed" / "extent"
pragma         = "@" keyword

Then I asked Chat to convert it to LPEG format. I haven't tried this yet but I'm capturing it here in case Chat forgets our conversations again.

local lpeg = require "lpeg"
local P, R, S, C, Ct = lpeg.P, lpeg.R, lpeg.S, lpeg.C, lpeg.Ct

local grammar = {
  statement = P("pragma" + "comment" + "assignment" + "declaration"),
  assignment = Ct("type" * S(" \t\r\n")^0 * C("identifier") * S(" \t\r\n")^0 * "=" * S(" \t\r\n")^0 * "expression"),
  declaration = Ct("type" * S(" \t\r\n")^0 * C("identifier")),
  type = P("int" + "float" + "string" + "bool"),
  expression = P("literal" + "identifier"),
  literal = P("hex" + "floating_point" + "integer" + "string" + "boolean"),
  integer = R("09")^1,
  floating_point = R("09")^1 * "." * R("09")^1,
  hex = "0x" * R("09", "af", "AF")^1,
  string = '"' * ((1 - S('\\"')) + "\\" * 1)^0 * '"',
  boolean = P("true" + "false"),
  identifier = R("az", "AZ") * R("az", "AZ", "09", "_")^0,
  comment = "#" * (1 - P("\n"))^0,
  keyword = P("sealed" + "extent"),
  pragma = "@" * C("keyword")
}

return grammar

@emrainey
Copy link
Collaborator Author

emrainey commented Sep 8, 2023

Also dropping a link to https://bford.info/packrat/ and for Lua's LPEG https://www.inf.puc-rio.br/~roberto/lpeg/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant