Created
March 24, 2016 16:21
-
-
Save anonymous/3f7d6a7d12c98cab0760 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/env perl6 | |
use v6; | |
grammar KeySymDefGrammar { | |
token upper { <[A..Z]> } | |
token lower { <[a..z]> } | |
token digit { <[0..9]> } | |
token alpha { <+upper+lower+[_]> } | |
token alnum { <+alpha+digit> } | |
token ident { <alpha><alnum>* } | |
token xdigit { <+digit+[A..Fa..f]> } | |
rule TOP { | |
<.ws> | |
( | |
|| <COMMENT> | |
|| <IFDEF> | |
|| <DEFINE> | |
)* | |
} | |
rule DEFINE { <DEFINE_OBJECT> || <DEFINE_SIMPLE> } | |
rule DEFINE_OBJECT { <DIRECTIVE('define')> <ident> <([<alnum>]+)> } | |
rule DEFINE_SIMPLE { <DIRECTIVE('define')> <ident> } | |
rule IFDEF { <DIRECTIVE('ifdef')> <(<ident>)> } | |
token DIRECTIVE ($type) { <.ws> "#" <.ws> $type } | |
token COMMENT { <SINGLE_LINE_COMMENT> || <MULTI_LINE_COMMENT> } | |
token SINGLE_LINE_COMMENT {'//' <(\N*)> } | |
token MULTI_LINE_COMMENT { | |
'/*' <( [ <-[ * ]> || '*' <!before '/'> ]* )> '*/' | |
} | |
} | |
class KeySymDefActions { | |
method TOP ($/) { | |
# make $0.map({ $_<STRING> // $_<CHAR> })».made.join(', '); | |
make $0.map({ $_<COMMENT> // $_<IFDEF> // $_<DEFINE> })».made; | |
} | |
method DEFINE ($/) { | |
my $c = $<DEFINE_SIMPLE> // $<DEFINE_OBJECT>; | |
make $c.made; | |
} | |
method DEFINE_OBJECT ($/) { | |
make ( define => ( ~$<ident> => ~$() ) ); | |
} | |
method DEFINE_SIMPLE ($/) { | |
make ( define => ~$<ident> ); | |
} | |
method IFDEF ($/) { | |
make ( ifdef => ~$() ); | |
} | |
method COMMENT ($/) { | |
my $c = $<SINGLE_LINE_COMMENT> // $<MULTI_LINE_COMMENT>; | |
make ( comment => ~$c.trim() ); | |
} | |
} | |
my $ksdg = KeySymDefGrammar.new; | |
my $ksda = KeySymDefActions.new; | |
my $doc = q :to 'EOF'; | |
#ifdef ABC | |
#define XYZ | |
#define asdf HJKL | |
// Single-line 1. | |
/* Multi-line A. */ | |
/* Multi-line B. */ | |
// Single-line 2. | |
EOF | |
use Data::Dump; | |
$ksdg.parse($doc, :actions($ksda)).made».say |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment