Skip to content

Commit

Permalink
Ease up on the docs + add Inch CI badge to README
Browse files Browse the repository at this point in the history
  • Loading branch information
patgrasso committed Aug 4, 2016
1 parent f93be49 commit a948629
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 79 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
[![NPM Version][npm-image]][npm-url]
[![Build Status][travis-image]][travis-url]
[![Coverage Status][coveralls-image]][coveralls-url]
[![Inline Docs][inch-ci-image]][inch-ci-url]

Parsey is a parser for context-free grammars. It utilizes
[earley parsing](https://www.wikiwand.com/en/Earley_parser), a top-down chart
Expand Down Expand Up @@ -92,7 +93,7 @@ The CFG is a glorified array, a container for your rules. It has some methods
like `rule()` and `getSymbols()`, which can be handy for creating rules from
strings.

```javascsript
```javascript
const CFG = require('parsey').CFG;
let grammar = new CFG();

Expand Down Expand Up @@ -157,7 +158,7 @@ Example:


## Examples
Check out [examples](/patgrasso/parsey/tree/master/examples) to see parsey in
Check out [examples](https://github.com/patgrasso/parsey/tree/master/examples) to see parsey in
action for various use cases!


Expand All @@ -176,4 +177,6 @@ MIT
[travis-url]: https://travis-ci.org/patgrasso/parsey
[coveralls-image]: https://coveralls.io/repos/github/patgrasso/parsey/badge.svg?branch=master
[coveralls-url]: https://coveralls.io/github/patgrasso/parsey?branch=master
[inch-ci-image]: https://inch-ci.org/github/patgrasso/parsey.svg?branch=master
[inch-ci-url]: https://inch-ci.org/github/patgrasso/parsey

13 changes: 2 additions & 11 deletions lib/cfg.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,7 @@
const Rule = require('./rules').Rule;
const Sym = require('./rules').Sym;

/**
* Regex that matches bare JS regex expressions like /^\d+$/g
* @constant {RegExp} REGEX_REGEX
*/
const REGEX_REGEX = new RegExp('^\/(.*)\/([gimy]*)$');

/**
* Regex that matches bare JS string expressions like "hello" and 'hello'
* @constant {RegExp} STR_REGEX
*/
const STR_REGEX = new RegExp('^\'(.*)\'|"(.*)"$');


Expand All @@ -30,8 +21,8 @@ const STR_REGEX = new RegExp('^\'(.*)\'|"(.*)"$');
* @class CFG
* @extends Array
* @constructor
* @param {rules=} - Optional array of {@link module:lib/rules.Rule|Rules} to
* initialize the grammar with
* @param {rules=} - Optional array of {@link Rule}s to initialize the grammar
* with
*/
function CFG(rules) {
let arr = [];
Expand Down
47 changes: 16 additions & 31 deletions lib/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,10 @@ const tokenize = require('./tokenizer');
* @function parse
* @memberof module:lib/parser
* @param {string} sent - Input string to parse
* @param {module:lib/rules~Rule[]} grammar - Set of
* {@link module:lib/rules~Rule|Rules} that define a language
* @param {Function} [tokenizer={@link module:lib/tokenizer|tokenize}] -
* Function that accepts a string and a grammar (optionally) and splits
* the input string into tokens, each representing a symbol in the
* language
* @param {Rule[]|CFG} grammar - Set of Rules that define a language
* @param {Function} [tokenizer=#tokenize} - Function that accepts a string and
* a grammar (optionally) and splits the input string into tokens, each
* representing a symbol in the language
* @return {object} Root node of the parse tree
*/
function parse(sent, grammar, tokenizer) {
Expand All @@ -44,8 +42,7 @@ function parse(sent, grammar, tokenizer) {
* @function earley
* @memberof module:lib/parser
* @param {string[]} tokens - Sequence of symbols to be parsed
* @param {module:lib/rules~Rule[]} grammar - Set of rules that define a
* language
* @param {Rule[]|CFG} grammar - Set of rules that define a language
* @return {state[]} Set of 'states', each of which contains a list of earley
* items. Each earley item looks something like this:
* <pre><code>
Expand Down Expand Up @@ -90,9 +87,8 @@ function earley(tokens, grammar) {


/**
* Prediction stage in the earley algorithm. Given an earley item, determine
* if the next symbol to be processed is a non-terminal, and if so add all
* rules whose LHS equals that symbol to the current earley state.
* Prediction stage in the earley algorithm
* {@link https://loup-vaillant.fr/tutorials/earley-parsing/recogniser}
*
* This also avoids adding duplicate rules to a state, a pitfall caused by
* left-recursive grammars
Expand All @@ -102,8 +98,7 @@ function earley(tokens, grammar) {
* @param {state[]} states - Set of lists of earley items
* @param {number} i - Index of the earley state to be processed
* @param {number} j - Index of the earley item to be processed within the state
* @param {module:lib/rules~Rule[]} grammar - Set of rules that define the
* language
* @param {Rule[]|CFG} grammar
*/
function predict(tokens, states, i, j, grammar) {
let curr = states[i][j];
Expand All @@ -130,11 +125,8 @@ function predict(tokens, states, i, j, grammar) {


/**
* Scanning stage in the earley algorithm. Given an earley item, determine if
* the next symbol to be processed is a terminal, and if so see if it matches
* the tokens at the state/index described by `i`. If the token matches, add
* an earley item to the next state that is a duplicate of this one, except
* whose position is one greater
* Scanning stage in the earley algorithm
* {@link https://loup-vaillant.fr/tutorials/earley-parsing/recogniser}
*
* @function scan
* @param {string[]} tokens - Input tokens being parsed
Expand Down Expand Up @@ -167,14 +159,8 @@ function scan(tokens, states, i, j) {


/**
* Completion stage in the earley algorithm. If the current earley item's
* position is >= to the length of it's rule, it has successfully parsed the
* rule it represents!
*
* Once this has occurred, go back to the state in which the earley item
* originated and find all earley items whose next symbol to match matches our
* earley item, and add them to the current state, incrementing their positions
* accordingly
* Completion stage in the earley algorithm
* {@link https://loup-vaillant.fr/tutorials/earley-parsing/recogniser}
*
* @function complete
* @param {string[]} tokens - Input tokens being parsed
Expand Down Expand Up @@ -226,8 +212,8 @@ function removeUnfinishedItems(states) {

/**
* Places earley items in the states in which they originated, as opposed to the
* states in which they finished parsing, and set their <code>origin</code>
* properties to the state in which they finished.
* states in which they finished parsing, and set their `origin` properties to
* the state in which they finished.
*
* This allows a depth-first search of the chart to move forwards through the
* graph, which is more intuitive than having to move backwards
Expand All @@ -252,9 +238,8 @@ function swap(states) {


/**
* Performs a depth-first search on the chart generated by
* {@link module:lib/parser~earley|earley()} in order to construct a parse tree,
* an example of which is shown below
* Performs a depth-first search on the chart generated by {@link #earley()} in
* order to construct a parse tree, an example of which is shown below
*
* @example
* {
Expand Down
17 changes: 5 additions & 12 deletions lib/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@
* @extends Array
* @constructor
* @memberof module:lib/rules
* @param {module:lib/rules~Sym} lhs - {@link module:lib/rules~Sym|Sym}
* representing the left hand side of the production
* @param {Array.<module:lib/rules~Sym|string|RegExp>} rhs - Sequence of
* {@link module:lib/rules~Sym|Syms}, plain strings, or RegExp objects that
* represents the right hand side of the production
* @param {Sym} lhs - Sym representing the left hand side of the production
* @param {Array.<Sym|string|RegExp>} rhs - Sequence of Syms, plain strings, or
* RegExp objects that represents the right hand side of the production
* @param {Function=} valuator - Function used to evaluate values obtained by
* matching this production
*/
Expand Down Expand Up @@ -68,11 +66,6 @@ function Sym(name) {
}


module.exports = {
/** @see {@link module:lib/rules.Rule|Rule} */
Rule : Rule,

/** @see {@link module:lib/rules.Sym|Sym} */
Sym : Sym
};
module.exports.Rule = Rule;
module.exports.Sym = Sym;

3 changes: 1 addition & 2 deletions lib/tokenizer.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
*
* @function
* @param {string} sent - Sentence or string to be split/tokenized
* @param {Array.<module:lib/rules~Rule|Rule>} grammar - List of
* [Rules]{@link module:lib/rules~Rule} that define the grammar
* @param {Rule[]|CFG} grammar - Set of Rules that define a language
* @return {string[]} Tokens/the sentence, split by each terminal character
* found within the grammar
*/
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"lint": "eslint . --ignore-pattern coverage",
"test": "jasmine",
"coverage": "istanbul cover --include-all-sources -x **/spec/** -x **/examples/** jasmine",
"coveralls": "istanbul cover --include-all-sources -x **/spec/** jasmine --captureExceptions && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage"
"coveralls": "istanbul cover --include-all-sources -x **/spec/** -x **/examples/** jasmine --captureExceptions && cat ./coverage/lcov.info | ./node_modules/coveralls/bin/coveralls.js && rm -rf ./coverage"
},
"repository": {
"type": "git",
Expand Down
21 changes: 1 addition & 20 deletions parsey.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,14 @@
/**
* Export module for the parsey package
* Exports for parsey
*
* @module parsey
*/

module.exports = {
/**
* @see module:lib/parser.parse
*/
parse : require('./lib/parser').parse,

/**
* @see module:lib/tokenizer
*/
tokenize : require('./lib/tokenizer'),

/**
* @see module:lib/rules.Rule
*/
Rule : require('./lib/rules').Rule,

/**
* @see module:lib/rules.Sym
*/
Sym : require('./lib/rules').Sym,

/**
* @see module:lib/cfg
*/
CFG : require('./lib/cfg')
};

0 comments on commit a948629

Please sign in to comment.