Skip to content

Instantly share code, notes, and snippets.

@Enome
Forked from anonymous/gist:4381280
Created December 26, 2012 16:30
Show Gist options
  • Save Enome/4381288 to your computer and use it in GitHub Desktop.
Save Enome/4381288 to your computer and use it in GitHub Desktop.
%lex
%%
\n+ { process.stdout.write('[ENDLINE(S)]\n'); return 'ENDLINE'; }
<<EOF>> { process.stdout.write('[EOF]'); return 'EOF'; }
(.*?)":" { process.stdout.write('[KEY]'); return 'KEY'; }
\s { process.stdout.write('[SPACE]'); return 'SPACE'; }
[^\Z|\n]+ { process.stdout.write('[VALUE]'); return 'VALUE'; }
/lex
%%
start
: KEY SPACE VALUE ENDLINE
| VALUE ENDLINE
| KEY SPACE VALUE EOF
;
var input = 'amount: 200 ml \n' +
'size:300\n' +
'\n' +
'This is just a comment I made.\n' +
'temp: 20celsius';
var output = [
[
{ key: 'amount', value: '200 ml' },
{ key: 'size', value: '300 ml' },
],
'\n',
'This is a comment',
'\n',
[
{ key: 'temp', value: '20celsius' },
],
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment