Created
December 27, 2019 21:43
-
-
Save russ/04853f424fb1004056070b94657b9449 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/bash | |
pegasus < parser.grammar > parser.json | |
pegasus-crystalsem -l parser.json -a parser.sem -s parser.cr | |
crystal test.cr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
token whitespace = /[ \n\t]+/ [ skip ]; | |
token SLASH = /\//; | |
token LITERAL = /[A-Za-z0-9_]+/; | |
token DOT = /\./; | |
token SYMBOL = /:[A-Za-z0-9_]+/; | |
token STAR = /\*[A-Za-z0-9_]+/; | |
token LPAREN = /\(/; | |
token RPAREN = /\)/; | |
token OR = /\|/; | |
rule S = expressions; | |
rule expressions = expressions expression | expression | or; | |
rule expression = terminal | group | STAR; | |
rule terminal = SYMBOL | SLASH | LITERAL | DOT; | |
rule group = LPAREN expressions RPAREN; | |
rule or = expressions OR expression; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type token = $$ Token $$ | |
typerules token = [S, expressions, expression, terminal, group, or] | |
init = $$ $$ | |
rule S(0) = $$ $out = $0 $$ | |
rule expressions(0) = $$ $out = $0 $$ | |
rule expressions(1) = $$ $out = $0 $$ | |
rule expressions(2) = $$ $out = $0 $$ | |
rule expression(0) = $$ $out = $0 $$ | |
rule expression(1) = $$ $out = $0 $$ | |
rule expression(2) = $$ $out = $0 $$ | |
rule terminal(0) = $$ $out = $0 $$ | |
rule terminal(1) = $$ $out = $0 $$ | |
rule terminal(2) = $$ $out = $0 $$ | |
rule terminal(3) = $$ $out = $0 $$ | |
rule group(0) = $$ $out = $0 $$ | |
rule or(0) = $$ $out = $0 $$ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "./parser.cr" | |
def print_tree(tree, indent = 0) | |
indent.times { STDOUT << " " } | |
case tree | |
when Pegasus::Generated::TerminalTree | |
STDOUT << "Terminal: " | |
STDOUT.puts tree.string | |
when Pegasus::Generated::NonterminalTree | |
STDOUT << "Nonterminal: " << tree.name | |
STDOUT.puts | |
tree.children.each { |it| print_tree(it, indent + 1) } | |
end | |
end | |
paths = [ | |
"/path/to/some.html", | |
"/path/to/some|other.html", | |
"/path/to/some|other.(:format)", | |
"/path/to(/track/(/*path))", | |
] | |
paths.each do |path| | |
puts "=" * 50 | |
puts path | |
puts "-" * 50 | |
# print_tree(Pegasus::Generated.process(path)) | |
puts Pegasus::Generated.process(path).inspect | |
puts "=" * 50 | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment