public
Last active

Javascript CSV Parser generated by PEG.js

  • Download Gist
csv.pegjs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
{
var separator = ',';
}
 
start
= comma
 
comma
= & { return separator = ','; } sv:sv { return sv; }
 
tab
= & { return separator = '\t'; } sv:sv { return sv; }
 
sv
= [\n\r]* first:line rest:([\n\r]+ data:line { return data; })* [\n\r]* { rest.unshift(first); return rest; }
 
line
= first:field rest:(char:. & { return char == separator; } text:field { return text; })*
& { return !!first || rest.length; }
{ rest.unshift(first); return rest; }
 
field
= '"' text:char* '"' { return text.join(''); }
/ text:(char:[^\n\r] & { return char != separator; } { return char; })*
{ return text.join(''); }
 
char
= '"' '"' { return '"'; }
/ [^"]

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.