Skip to content

Instantly share code, notes, and snippets.

@2colours
Last active September 29, 2023 18:29
Show Gist options
  • Save 2colours/94f345b3bd2e60238f8ee96832dcfdd5 to your computer and use it in GitHub Desktop.
Save 2colours/94f345b3bd2e60238f8ee96832dcfdd5 to your computer and use it in GitHub Desktop.
#!/usr/bin/env raku
use v6.d;
role Token {}
role EOF does Token {}
role WS does Token {}
role Dot does Token {}
role Colon does Token {}
role Hash does Token {}
role SingleQuote does Token {}
role DoubleQuote does Token {}
role Dash does Token {}
role Bar does Token {}
role Comma does Token {}
role LeftBrace does Token {}
role RightBrace does Token {}
role LeftSquare does Token {}
role RightSquare does Token {}
role LeftParen does Token {}
role RightParen does Token {}
role DotHash does Token {}
role DotLBrace does Token {}
role DotRBrace does Token {}
role Ident[Str \ident] does Token {
has Str $.ident = ident;
}
role Number[Int \i] does Token {
has Int $.i = i;
}
role Decimal[Rat \d] does Token {
has Rat $.d = d;
}
grammar Lexer {
token TOP { <tok>* }
proto token tok {*}
token tok:sym<whitespace> { <!ww> \s+ }
token tok:sym<dot> { '.' }
token tok:sym<colon> { ':' }
token tok:sym<hash> { '#' }
token tok:sym<squote> { '\'' }
token tok:sym<dquote> { '"' }
token tok:sym<dash> { '-' }
token tok:sym<bar> { '|' }
token tok:sym<comma> { ',' }
token tok:sym<lbrace> { '{' }
token tok:sym<rbrace> { '}' }
token tok:sym<lsquare> { '[' }
token tok:sym<rsquare> { ']' }
token tok:sym<lparen> { '(' }
token tok:sym<rparen> { ')' }
token tok:sym<dothash> { '.#' }
token tok:sym<dotlbrace> { '.{' }
token tok:sym<dotrbrace> { '.}' }
token tok:sym<ident> { <ident_part>+ }
token ident_part { \w || '_' }
}
class Tokenizer {
method tok:sym<whitespace> ($/) { make WS }
method tok:sym<dot> ($/) { make Dot }
method tok:sym<colon> ($/) { make Colon }
method tok:sym<hash> ($/) { make Hash }
method tok:sym<squote> ($/) { make SingleQuote }
method tok:sym<dquote> ($/) { make DoubleQuote }
method tok:sym<dash> ($/) { make Dash }
method tok:sym<bar> ($/) { make Bar }
method tok:sym<comma> ($/) { make Comma }
method tok:sym<lbrace> ($/) { make LeftBrace }
method tok:sym<rbrace> ($/) { make RightBrace }
method tok:sym<lsquare> ($/) { make LeftSquare }
method tok:sym<rsquare> ($/) { make RightSquare }
method tok:sym<lparen> ($/) { make LeftParen }
method tok:sym<rparen> ($/) { make RightParen }
method tok:sym<dothash> ($/) { make DotHash }
method tok:sym<dotlbrace> ($/) { make DotLBrace }
method tok:sym<dotrbrace> ($/) { make DotRBrace }
method tok:sym<ident> ($/) { make Ident[$<ident_part>.join].new }
method TOP ($match) { $match.make: $match<tok>>>.made }
}
my $match = Lexer.parse('.', actions => Tokenizer);
dd $match.made;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment