1.1.2 • Published 7 years ago

fastparse v1.1.2

Weekly downloads
4,614,253
License
MIT
Repository
github
Last release
7 years ago

fastparse

A very simple and stupid parser, based on a statemachine and regular expressions.

It's not intended for complex languages. It's intended to easily write a simple parser for a simple language.

Usage

Pass a description of statemachine to the constructor. The description must be in this form:

new Parser(description)

description is {
	// The key is the name of the state
	// The value is an object containing possible transitions
	"state-name": {
		// The key is a regular expression
		// If the regular expression matches the transition is executed
		// The value can be "true", a other state name or a function

		"a": true,
		// true will make the parser stay in the current state
		
		"b": "other-state-name",
		// a string will make the parser transit to a new state
		
		"[cde]": function(match, index, matchLength) {
			// "match" will be the matched string
			// "index" will be the position in the complete string
			// "matchLength" will be "match.length"
			
			// "this" will be the "context" passed to the "parse" method"
			
			// A new state name (string) can be returned
			return "other-state-name";
		},
		
		"([0-9]+)(\\.[0-9]+)?": function(match, first, second, index, matchLength) {
			// groups can be used in the regular expression
			// they will match to arguments "first", "second"
		},
		
		// the parser stops when it cannot match the string anymore
		
		// order of keys is the order in which regular expressions are matched
		// if the javascript runtime preserves the order of keys in an object
		// (this is not standardized, but it's a de-facto standard)
	}
}

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.

Parser.prototype.parse(initialState: String, parsedString: String, context: Object)

initialState: state where the parser starts to parse.

parsedString: the string which should be parsed.

context: an object which can be used to save state and results. Available as this in transition functions.

returns context

Example

var Parser = require("fastparse");

// A simple parser that extracts @licence ... from comments in a JS file
var parser = new Parser({
	// The "source" state
	"source": {
		// matches comment start
		"/\\*": "comment",
		"//": "linecomment",
		
		// this would be necessary for a complex language like JS
		// but omitted here for simplicity
		// "\"": "string1",
		// "\'": "string2",
		// "\/": "regexp"
		
	},
	// The "comment" state
	"comment": {
		"\\*/": "source",
		"@licen[cs]e\\s((?:[^*\n]|\\*+[^*/\n])*)": function(match, licenseText) {
			this.licences.push(licenseText.trim());
		}
	},
	// The "linecomment" state
	"linecomment": {
		"\n": "source",
		"@licen[cs]e\\s(.*)": function(match, licenseText) {
			this.licences.push(licenseText.trim());
		}
	}
});

var licences = parser.parse("source", sourceCode, { licences: [] }).licences;

console.log(licences);

License

MIT (http://www.opensource.org/licenses/mit-license.php)

css-selector-tokenizeroptimize-css-classnames-htmloptimize-css-classnames-angularjscloud-archive-s3fe-bless@olivervorasai/sliderasterjs@infinitebrahmanuniverse/nolb-fastp@everything-registry/sub-chunk-1651p149-tablevue-html-loaderwxbtemplate-url-loadersticky-scroll-catchthbs-loadervue-compment@ct-note/table@ct-note/inline-code@ct-note/marker@ct-note/quote@ct-note/simple-image@ct-note/checklist@ct-note/delimiter@ct-note/header@dinert/componentgenerator-bootstrap-boilerplate-templatejade-url-replace-loaderjcy-loaderjujin-dvjack-html-loaderkafirchain-tetrischain_diggersprites-loaderums-weex-loadertpl-html-loadervue-picocss@artiso-solutions/vue-html-to-paper@alta-ng-timeago/hiamtanh-alta@bitfirer/vue-qriously@carhoo/widget-dealers91jin@devorso/dcountdownxwy-dvxslt-template-loaderwebpack-sri-retrywebpack-retry-load-pluginunderscore-template-loader@hazyflame/vue-jitsi-meet@icetee/handlebars-loader@gitterhq/handlebars-loader@goldfishjs/pre-build@graines-digitales/nice-handsome-button@innersource.soprasteria.com/underscore-template-loader@kristoffertonning/vue-laravel-errors@leiyulf/gantt-task-react@isunkui/gulp-html-url-prefix-suffix@mpxjs/webpack-plugin@ngxvoice/ngx-voicelistner@enirisdev/angular-google-charts@mireksz/handlebars-loader@modul/extra-html-loader@jarrydark/editorsimplejs@kitten-team/webpack-retry-load-plugin@kommunicate/kommunicate-chatbot-plugin@sak1sham/react-toc-highlight@refactorpro/pixi-virtual-joystick@nurturecloud/html-loader@ornery/web-components@seesaw/html-loadermpregular-loadermkcp-solarpunkmggauharmireksz-handlebars-loadermiguelcostero-ng2-toastyp147-tablep148-tablepanorama-layout-loadersearch-list-reactsearch-input-reactrollup-plugin-ng-cachereact-misc-toolboxreact-lightbox-pack-18supportreadme-loaderregularjs-loaderregular-loadersaber-clireact-gantt-chart7nka-gantt-task-reactng-cache-loadernested-html-loaderng-search-dropdownngfactory-loadernois-react-toastplntr-handlebars-loaderreyhan-projectpure-loaderractive-html-loaderresponsis-gantt-task-reactsimplify-html-loaderhulix
1.1.2

7 years ago

1.1.1

10 years ago

1.1.0

10 years ago

1.0.0

11 years ago