Skip to content

Instantly share code, notes, and snippets.

@lamchau
Forked from jakubkulhan/indent.pegjs
Last active August 29, 2015 14:17
Show Gist options
  • Save lamchau/9fa50bdc6f15a0066614 to your computer and use it in GitHub Desktop.
Save lamchau/9fa50bdc6f15a0066614 to your computer and use it in GitHub Desktop.
// do not use result cache, nor line and column tracking
{ var indentStack = [], indent = ""; }
start
= INDENT? lines:( blank / line )*
{ return lines; }
line
= SAMEDENT line:(!EOL c:. { return c; })+ EOL?
children:( b:blank* INDENT c:( blank / line )* DEDENT { return b.concat(c); })?
{ return [line.join(""), children === "" ? [] : children]; }
blank
= [ \t]* EOL
{ return undefined; }
EOL
= "\r\n" / "\n" / "\r"
SAMEDENT
= i:[ \t]* &{ return i.join("") === indent; }
INDENT
= i:[ \t]+ &{ return i.length > indent.length; }
{ indentStack.push(indent); indent = i.join(""); pos = offset; }
DEDENT
= { indent = indentStack.pop(); }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment