Skip to content

Instantly share code, notes, and snippets.

@nikic

nikic/parser.php Secret

Last active December 15, 2015 00:19
Show Gist options
  • Save nikic/f5b1be5b009c124cd05a to your computer and use it in GitHub Desktop.
Save nikic/f5b1be5b009c124cd05a to your computer and use it in GitHub Desktop.
<?php
$lexer = $factory->createLexer([[
';(?:.*)(?:\\n)?' => 'comment',
'#_\s?\S+' => 'discard',
'[\s,]+' => 'whitespace',
'\\(' => 'list_start',
'\\)' => 'list_end',
'\\[' => 'vector_start',
'\\]' => 'vector_end',
'#\\{' => 'set_start',
'\\{' => 'map_start',
'\\}' => 'map_set_end',
'nil|true|false' => function($_, $matches) {
static $literals = ['nil' => null, 'true' => true, 'false' => false];
return ['literal', $literals[$matches[0]]];
},
'"([^"\\\\]*(?:\\\\.[^"\\\\]*)*)"' => function($_, $matches) {
$string = strtr($matches[1], [
'\t' => "\t",
'\r' => "\r",
'\n' => "\n",
'\"' => "\"",
]);
return ['string', $string];
},
'\\\\([a-z]+)' => function($_, $matches) {
static $chars = [
'newline' => "\n",
'return' => "\r",
'space' => ' ',
'tab' => "\t",
];
$name = $matches[1];
return ['character', isset($chars[$name]) ? $chars[$name] : $name];
},
'[+-]?[0-9]+\.[0-9]+M?' => function($_, $matches) {
return ['float', (float) $matches[0]];
},
'[+-]?[0-9]+N?' => function($_, $matches) {
return ['int', (int) $matches[0]];
},
get_symbol_regex() => function($_, $matches) {
return ['symbol', Symbol::get($matches[0])];
},
':('.get_symbol_regex().')' => function($_, $matches) {
return ['symbol', Keyword::get($matches[1])];
},
'#('.get_symbol_regex().')' => function($_, $matches) {
return ['symbol', new Tag($matches[1])];
},
]]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment