grammar.pegjs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. start
  2. = ws head:segment tail:segmentTail* { tail.unshift(head); return tail; }
  3. segmentTail
  4. = ws ',' ws seg:segment { return seg; }
  5. segment
  6. = str:string { return {string: str}; }
  7. / v:identifier size:size ? specs:specifierList ?
  8. { return {name: v, size: size, specifiers: specs}; }
  9. / v:number size:size ? specs:specifierList ?
  10. { return {value: v, size: size, specifiers: specs}; }
  11. string
  12. = '"' '"' { return ""; }
  13. / '"' chars:chars '"' { return chars; }
  14. /* From JSON example
  15. https://github.com/dmajda/pegjs/blob/master/examples/json.pegjs */
  16. chars
  17. = chars:char+ { return chars.join(""); }
  18. char
  19. = [^"\\\0-\x1F\x7f]
  20. / '\\"' { return '"'; }
  21. / "\\\\" { return "\\"; }
  22. / "\\/" { return "/"; }
  23. / "\\b" { return "\b"; }
  24. / "\\f" { return "\f"; }
  25. / "\\n" { return "\n"; }
  26. / "\\r" { return "\r"; }
  27. / "\\t" { return "\t"; }
  28. / "\\u" h1:hexDigit h2:hexDigit h3:hexDigit h4:hexDigit {
  29. return String.fromCharCode(parseInt("0x" + h1 + h2 + h3 + h4));
  30. }
  31. hexDigit
  32. = [0-9a-fA-F]
  33. identifier
  34. = (head:[_a-zA-Z] tail:[_a-zA-Z0-9]*) { return head + tail.join(''); }
  35. number
  36. = '0' { return 0; }
  37. / head:[1-9] tail:[0-9]* { return parseInt(head + tail.join('')); }
  38. size
  39. = ':' num:number { return num; }
  40. / ':' id:identifier { return id; }
  41. specifierList
  42. = '/' head:specifier tail:specifierTail* { tail.unshift(head); return tail; }
  43. specifierTail
  44. = '-' spec:specifier { return spec; }
  45. specifier
  46. = 'little' / 'big' / 'signed' / 'unsigned'
  47. / 'integer' / 'binary' / 'float'
  48. / unit
  49. unit
  50. = 'unit:' num:number { return 'unit:' + num; }
  51. ws = [ \t\n]*