Skip to content

Instantly share code, notes, and snippets.

@skids
Created May 18, 2012 00:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save skids/2722431 to your computer and use it in GitHub Desktop.
Save skids/2722431 to your computer and use it in GitHub Desktop.
Random thought on emitting from Perl6 grammars
# So for building ASTs we already have something like:
class Actions {
method foo { make $0 };
#etc
}
# The complement would look something like this, using the same convention
# of matching names that appear as Grammar rules.
#
# The subtle concept here is that this class, in addition to the just code
# in it, when viewed as a list of longnames, provides instructions for
# re-emitting.
class Emit {
multi method foo (Match $m) {...}; # For re-emitting cleaner after a match
multi method foo (ReMatch $r) {...}; # Hypothetically the below might generate
# stuff and send it back through these
multi method foo (*%stuff) {...}; # For emitting from stripped AST/objs
}
# Just as an idiom, you can already easily add a naive emit method
grammar EmittiveGrammar {
method emit($source, :$rule = TOP, :$emit, *%opt) {
$emit."$rule"($source);
}
# other normal grammar stuff
}
# This at least lets you carry emitters around with the grammar, overload
# them, etc., as one can do with the class fed to .parse(:actions)
#
# But of course the "real thing" would need to be intelligent enough to
# add value to storing those methods in the Emit class, by reducing the
# amount of coding necessary to produce emitters. Or in other words,
# somehow be easier to use than just rolling your own.
#
# The purpose of the above is to re-emit which means:
# 1) Even with a stored Match, one must put back uncollected delimiters
# 2) Perhaps restoring a normative order to the Match's hash/list
# 3) In the case of ASTs or a gaggle of objects, traversing them in some
# sensible order.
#
# ...so the emit function would have to introspect the Grammar rules and the
# signatures supplied in the Emit class and based on that, decide
# when or if to call given methods in some intelligent if not prescient
# manner :-). And, do all that without requiring a 50 page instruction
# manual.
#
# Minor bit of tackiness: potential for confusion between reconstructing
# from a Match versus reconstructing from an AST/Objects if you somehow
# manage to mix foreign Match objects into your AST/Objects.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment