PistonDevelopers / meta

A DSL parsing library for human readable text documents
MIT License
90 stars 5 forks source link

Add support for "generic rules" #276

Closed bvssvni closed 8 years ago

bvssvni commented 9 years ago

Currently, there is no way to reuse a rule pattern.

For example, if we have a rule for parsing a struct:

0 struct = ["struct" .w! .t!:"name" .w? "{" .w? .s?.([.w? "," .w?]
  [.t!:"field_name" .w? ":" .w? .t!:"field_type"]
) .w? "}"]

A "generic rule" would allow you to pass rules as arguments and change aspect of a rule.

For example:

0 fields(prefix start end) = [prefix .w? start .w? .s?.([.w? "," .w?]
  [.t!:"field_name" .w? ":" .w? .t!:"field_type"]
) .w? end]
1 struct = fields(["struct" .w! .t!:"name"] "{" "}")
2 dictionary = fields(.w? "{" "}")
bvssvni commented 9 years ago

I haven't yet had a use case where this would help a lot. It could improve structure accuracy, but this usually does not matter. The problem piston-meta solves is to parse text where you else would need to write a parser, and the text is usually structured similar to JSON. With other words, no complex formats.