You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
3.1 KiB

4 years ago
  1. # fastparse
  2. A very simple and stupid parser, based on a statemachine and regular expressions.
  3. It's not intended for complex languages. It's intended to easily write a simple parser for a simple language.
  4. ## Usage
  5. Pass a description of statemachine to the constructor. The description must be in this form:
  6. ``` javascript
  7. new Parser(description)
  8. description is {
  9. // The key is the name of the state
  10. // The value is an object containing possible transitions
  11. "state-name": {
  12. // The key is a regular expression
  13. // If the regular expression matches the transition is executed
  14. // The value can be "true", a other state name or a function
  15. "a": true,
  16. // true will make the parser stay in the current state
  17. "b": "other-state-name",
  18. // a string will make the parser transit to a new state
  19. "[cde]": function(match, index, matchLength) {
  20. // "match" will be the matched string
  21. // "index" will be the position in the complete string
  22. // "matchLength" will be "match.length"
  23. // "this" will be the "context" passed to the "parse" method"
  24. // A new state name (string) can be returned
  25. return "other-state-name";
  26. },
  27. "([0-9]+)(\\.[0-9]+)?": function(match, first, second, index, matchLength) {
  28. // groups can be used in the regular expression
  29. // they will match to arguments "first", "second"
  30. },
  31. // the parser stops when it cannot match the string anymore
  32. // order of keys is the order in which regular expressions are matched
  33. // if the javascript runtime preserves the order of keys in an object
  34. // (this is not standardized, but it's a de-facto standard)
  35. }
  36. }
  37. ```
  38. The statemachine is compiled down to a single regular expression per state. So basically the parsing work is delegated to the (native) regular expression logic of the javascript runtime.
  39. ``` javascript
  40. Parser.prototype.parse(initialState: String, parsedString: String, context: Object)
  41. ```
  42. `initialState`: state where the parser starts to parse.
  43. `parsedString`: the string which should be parsed.
  44. `context`: an object which can be used to save state and results. Available as `this` in transition functions.
  45. returns `context`
  46. ## Example
  47. ``` javascript
  48. var Parser = require("fastparse");
  49. // A simple parser that extracts @licence ... from comments in a JS file
  50. var parser = new Parser({
  51. // The "source" state
  52. "source": {
  53. // matches comment start
  54. "/\\*": "comment",
  55. "//": "linecomment",
  56. // this would be necessary for a complex language like JS
  57. // but omitted here for simplicity
  58. // "\"": "string1",
  59. // "\'": "string2",
  60. // "\/": "regexp"
  61. },
  62. // The "comment" state
  63. "comment": {
  64. "\\*/": "source",
  65. "@licen[cs]e\\s((?:[^*\n]|\\*+[^*/\n])*)": function(match, licenseText) {
  66. this.licences.push(licenseText.trim());
  67. }
  68. },
  69. // The "linecomment" state
  70. "linecomment": {
  71. "\n": "source",
  72. "@licen[cs]e\\s(.*)": function(match, licenseText) {
  73. this.licences.push(licenseText.trim());
  74. }
  75. }
  76. });
  77. var licences = parser.parse("source", sourceCode, { licences: [] }).licences;
  78. console.log(licences);
  79. ```
  80. ## License
  81. MIT (http://www.opensource.org/licenses/mit-license.php)