OpenCyphal / wireshark_plugins

A place to hold WireShark Filters for Cyphal
MIT License
1 stars 0 forks source link

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

Open emrainey opened 1 year ago

emrainey commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

Converting an EBNF to PEG https://stackoverflow.com/questions/53053899/convert-ebnf-grammar-to-peg

http://www.romanredz.se/papers/FI2013.pdf

emrainey commented 1 year ago

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 commented 1 year ago

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