{"id":13656906,"url":"https://github.com/webpack/fastparse","last_synced_at":"2025-04-05T17:08:46.103Z","repository":{"id":19509791,"uuid":"22756490","full_name":"webpack/fastparse","owner":"webpack","description":"A very simple and stupid parser, based on a statemachine and regular expressions.","archived":false,"fork":false,"pushed_at":"2018-10-30T14:35:22.000Z","size":11,"stargazers_count":65,"open_issues_count":2,"forks_count":8,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-10-29T14:58:04.785Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/webpack.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-08-08T12:03:39.000Z","updated_at":"2023-11-13T07:45:15.000Z","dependencies_parsed_at":"2022-08-23T20:30:20.119Z","dependency_job_id":null,"html_url":"https://github.com/webpack/fastparse","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webpack%2Ffastparse","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webpack%2Ffastparse/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webpack%2Ffastparse/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/webpack%2Ffastparse/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/webpack","download_url":"https://codeload.github.com/webpack/fastparse/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247366648,"owners_count":20927556,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-08-02T05:00:34.205Z","updated_at":"2025-04-05T17:08:46.079Z","avatar_url":"https://github.com/webpack.png","language":"JavaScript","readme":"# fastparse\n\nA very simple and stupid parser, based on a statemachine and regular expressions.\n\nIt's not intended for complex languages. It's intended to easily write a simple parser for a simple language.\n\n\n\n## Usage\n\nPass a description of statemachine to the constructor. The description must be in this form:\n\n``` javascript\nnew Parser(description)\n\ndescription is {\n\t// The key is the name of the state\n\t// The value is an object containing possible transitions\n\t\"state-name\": {\n\t\t// The key is a regular expression\n\t\t// If the regular expression matches the transition is executed\n\t\t// The value can be \"true\", a other state name or a function\n\n\t\t\"a\": true,\n\t\t// true will make the parser stay in the current state\n\t\t\n\t\t\"b\": \"other-state-name\",\n\t\t// a string will make the parser transit to a new state\n\t\t\n\t\t\"[cde]\": function(match, index, matchLength) {\n\t\t\t// \"match\" will be the matched string\n\t\t\t// \"index\" will be the position in the complete string\n\t\t\t// \"matchLength\" will be \"match.length\"\n\t\t\t\n\t\t\t// \"this\" will be the \"context\" passed to the \"parse\" method\"\n\t\t\t\n\t\t\t// A new state name (string) can be returned\n\t\t\treturn \"other-state-name\";\n\t\t},\n\t\t\n\t\t\"([0-9]+)(\\\\.[0-9]+)?\": function(match, first, second, index, matchLength) {\n\t\t\t// groups can be used in the regular expression\n\t\t\t// they will match to arguments \"first\", \"second\"\n\t\t},\n\t\t\n\t\t// the parser stops when it cannot match the string anymore\n\t\t\n\t\t// order of keys is the order in which regular expressions are matched\n\t\t// if the javascript runtime preserves the order of keys in an object\n\t\t// (this is not standardized, but it's a de-facto standard)\n\t}\n}\n```\n\nThe 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.\n\n\n``` javascript\nParser.prototype.parse(initialState: String, parsedString: String, context: Object)\n```\n\n`initialState`: state where the parser starts to parse.\n\n`parsedString`: the string which should be parsed.\n\n`context`: an object which can be used to save state and results. Available as `this` in transition functions.\n\nreturns `context`\n\n\n\n\n## Example\n\n``` javascript\nvar Parser = require(\"fastparse\");\n\n// A simple parser that extracts @licence ... from comments in a JS file\nvar parser = new Parser({\n\t// The \"source\" state\n\t\"source\": {\n\t\t// matches comment start\n\t\t\"/\\\\*\": \"comment\",\n\t\t\"//\": \"linecomment\",\n\t\t\n\t\t// this would be necessary for a complex language like JS\n\t\t// but omitted here for simplicity\n\t\t// \"\\\"\": \"string1\",\n\t\t// \"\\'\": \"string2\",\n\t\t// \"\\/\": \"regexp\"\n\t\t\n\t},\n\t// The \"comment\" state\n\t\"comment\": {\n\t\t\"\\\\*/\": \"source\",\n\t\t\"@licen[cs]e\\\\s((?:[^*\\n]|\\\\*+[^*/\\n])*)\": function(match, licenseText) {\n\t\t\tthis.licences.push(licenseText.trim());\n\t\t}\n\t},\n\t// The \"linecomment\" state\n\t\"linecomment\": {\n\t\t\"\\n\": \"source\",\n\t\t\"@licen[cs]e\\\\s(.*)\": function(match, licenseText) {\n\t\t\tthis.licences.push(licenseText.trim());\n\t\t}\n\t}\n});\n\nvar licences = parser.parse(\"source\", sourceCode, { licences: [] }).licences;\n\nconsole.log(licences);\n```\n\n\n\n## License\n\nMIT (http://www.opensource.org/licenses/mit-license.php)\n","funding_links":[],"categories":["JavaScript"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebpack%2Ffastparse","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwebpack%2Ffastparse","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwebpack%2Ffastparse/lists"}