## Classes
DecisionGraph
GrammarGraph
GuidedDecisionGraph
Recognizer
## Functions
noSelfDefinitions(grammar, returns)

check a grammar for direct self-loops

parseGrammar(grammar, [seperator])DecisionGraph

parse a grammar given as an object and compile it into a decision graph

reduceGrammar(grammar, [seperator])object

reduces the rules of a grammar into a one to one form by assigning a name to all non-terminals. The end result is that each option on a rule with more than one choice will either be a single AND-rule or a single terminal.

clone(obj)object | array

helper function to clone a simple object/array made up of primitives. Will not work if the object or array contains non-primitives.

## Typedefs
SymbolChain : string

a string of one or more symbol names seperated by whitespace or another user defined seperator (see: seperator param for GrammarGraph)

Grammar : Object

a user defined context-free grammar formatted as an object consisting of key-value pairs, with each non-terminal symbol pointing to an array of one or more symbol chains choices for this non-terminal.

TreeNode : object
## DecisionGraph **Kind**: global class * [DecisionGraph](#DecisionGraph) * [new DecisionGraph()](#new_DecisionGraph_new) * [.addVertexAND(name)](#DecisionGraph+addVertexAND) * [.addVertexOR(name)](#DecisionGraph+addVertexOR) * [.addEdge(v, w)](#DecisionGraph+addEdge) * [.adj(v)](#DecisionGraph+adj) ⇒ Array.<string> * [.V()](#DecisionGraph+V) ⇒ number * [.isTerminal(v)](#DecisionGraph+isTerminal) ⇒ boolean * [.isVertex(v)](#DecisionGraph+isVertex) ⇒ boolean * [.isTypeAND(v)](#DecisionGraph+isTypeAND) ⇒ boolean * [.vertices()](#DecisionGraph+vertices) ⇒ Array.<string> ### new DecisionGraph() creates a new DecisionGraph ### decisionGraph.addVertexAND(name) add AND vertex to the graph. When moving through the decision graph, an AND vertex will require a visit down each of its outgoing edges, in the order the edges were added. **Kind**: instance method of [DecisionGraph](#DecisionGraph) **See**: [addVertexOR](#DecisionGraph+addVertexOR) | Param | Type | Description | | --- | --- | --- | | name | string | the name of this vertex | ### decisionGraph.addVertexOR(name) add OR vertex to the graph. When moving through the decision graph, an OR vertex chooses just one of its outgoing vertices. **Kind**: instance method of [DecisionGraph](#DecisionGraph) **See**: [addVertexAND](#DecisionGraph+addVertexAND) | Param | Type | Description | | --- | --- | --- | | name | string | the name of this vertex | ### decisionGraph.addEdge(v, w) add edge v->w to the graph **Kind**: instance method of [DecisionGraph](#DecisionGraph) | Param | Type | Description | | --- | --- | --- | | v | string | the name of a vertex this edge points from | | w | string | Array.<string> | the name of a vertex this edge points to or an array of vertex names. If vertex v is type AND, the order of w will be the exact order required. | ### decisionGraph.adj(v) ⇒ Array.<string> get an array of all the vertices this vertex points to **Kind**: instance method of [DecisionGraph](#DecisionGraph) **Returns**: Array.<string> - an ordered list of all the vertices that v points to | Param | Type | Description | | --- | --- | --- | | v | string | the name of a vertex | ### decisionGraph.V() ⇒ number get the number of vertices in this graph **Kind**: instance method of [DecisionGraph](#DecisionGraph) **Returns**: number - the number of vertices in this graph ### decisionGraph.isTerminal(v) ⇒ boolean is this a terminal vertex (does it have no outgoing edges?) **Kind**: instance method of [DecisionGraph](#DecisionGraph) **Returns**: boolean - is this a terminal vertex | Param | Type | Description | | --- | --- | --- | | v | string | the name of a vertex | ### decisionGraph.isVertex(v) ⇒ boolean is this the name of a vertex in the graph? **Kind**: instance method of [DecisionGraph](#DecisionGraph) **Returns**: boolean - is this a vertex in the graph | Param | Type | Description | | --- | --- | --- | | v | string | the name of a vertex | ### decisionGraph.isTypeAND(v) ⇒ boolean is this a type AND vertex (and not a type OR)? **Kind**: instance method of [DecisionGraph](#DecisionGraph) **Returns**: boolean - is this a type AND vertex (and not a type OR)? | Param | Type | Description | | --- | --- | --- | | v | string | the name of a vertex | ### decisionGraph.vertices() ⇒ Array.<string> get an array of vertex names **Kind**: instance method of [DecisionGraph](#DecisionGraph) **Returns**: Array.<string> - the vertex names in this graph ## GrammarGraph **Kind**: global class * [GrammarGraph](#GrammarGraph) * [new GrammarGraph(grammar, [seperator], [epsilonSymbol])](#new_GrammarGraph_new) * [.vertices()](#GrammarGraph+vertices) ⇒ Array.<string> * [.adj(v)](#GrammarGraph+adj) ⇒ Array.<string> * [.isTypeAND(v)](#GrammarGraph+isTypeAND) ⇒ boolean * [.createGuide(start)](#GrammarGraph+createGuide) ⇒ [GuidedDecisionGraph](#GuidedDecisionGraph) * [.createRecognizer(start)](#GrammarGraph+createRecognizer) ⇒ [Recognizer](#Recognizer) ### new GrammarGraph(grammar, [seperator], [epsilonSymbol]) creates a new GrammarGraph which can generate guides. | Param | Type | Default | Description | | --- | --- | --- | --- | | grammar | [Grammar](#Grammar) | | an object representing a grammar. | | [seperator] | string | RegExp | "/\\s+/" | how tokens will be divided in rules | | [epsilonSymbol] | string | "''" | Special terminal symbol that indicates this is an end of a construction. Defaults to the empty string. | ### grammarGraph.vertices() ⇒ Array.<string> get an array of vertex names in the graph **Kind**: instance method of [GrammarGraph](#GrammarGraph) **Returns**: Array.<string> - the vertex names in this graph **See**: [vertices](#DecisionGraph+vertices) ### grammarGraph.adj(v) ⇒ Array.<string> get an array of all the vertices this vertex points to **Kind**: instance method of [GrammarGraph](#GrammarGraph) **Returns**: Array.<string> - an ordered list of all the vertices that v points to **See**: [adj](#DecisionGraph+adj) | Param | Type | Description | | --- | --- | --- | | v | string | the name of a vertex | ### grammarGraph.isTypeAND(v) ⇒ boolean is this a type AND vertex (and not a type OR)? **Kind**: instance method of [GrammarGraph](#GrammarGraph) **Returns**: boolean - is this a type AND vertex (and not a type OR)? **See**: [isTypeAND](#DecisionGraph+isTypeAND) | Param | Type | Description | | --- | --- | --- | | v | string | the name of a vertex | ### grammarGraph.createGuide(start) ⇒ [GuidedDecisionGraph](#GuidedDecisionGraph) get a new GuidedDecisionGraph using this decision graph **Kind**: instance method of [GrammarGraph](#GrammarGraph) **Returns**: [GuidedDecisionGraph](#GuidedDecisionGraph) - a new guide from the provided start point **See**: [GuidedDecisionGraph](#GuidedDecisionGraph) for the methods available on the Guide | Param | Type | Description | | --- | --- | --- | | start | string | the name of a vertex in the decision graph from which to start the guided expansion | ### grammarGraph.createRecognizer(start) ⇒ [Recognizer](#Recognizer) Returns a new Recognizer from the given start vertex **Kind**: instance method of [GrammarGraph](#GrammarGraph) **Returns**: [Recognizer](#Recognizer) - a new Recognizer | Param | Type | Description | | --- | --- | --- | | start | string | the name of a vertex in the decision graph from which to start the recognizer test | ## GuidedDecisionGraph **Kind**: global class * [GuidedDecisionGraph](#GuidedDecisionGraph) * [new GuidedDecisionGraph(dg, start)](#new_GuidedDecisionGraph_new) * [.construction()](#GuidedDecisionGraph+construction) ⇒ Array.<string> * [.isComplete()](#GuidedDecisionGraph+isComplete) ⇒ boolean * [.choose(terminal)](#GuidedDecisionGraph+choose) * [.constructs()](#GuidedDecisionGraph+constructs) ⇒ Array.<string> * [.pop()](#GuidedDecisionGraph+pop) ⇒ string * [.choices([nDeep])](#GuidedDecisionGraph+choices) ⇒ Array.<string> | [Array.<TreeNode>](#TreeNode) ### new GuidedDecisionGraph(dg, start) step-by-step construction of a language from a decision graph | Param | Type | Description | | --- | --- | --- | | dg | [DecisionGraph](#DecisionGraph) | a Decision Graph that defines a grammar | | start | string | the name of a vertex in the decision graph from which to start the guided expansion | ### guidedDecisionGraph.construction() ⇒ Array.<string> the current construction **Kind**: instance method of [GuidedDecisionGraph](#GuidedDecisionGraph) **Returns**: Array.<string> - a terminal symbol chain ### guidedDecisionGraph.isComplete() ⇒ boolean is the current construction a valid, complete construction from the starting nonterminal? ie, could the construction be haulted at this point? Depending on the grammar, this may be true even if there are more choices at this point. **Kind**: instance method of [GuidedDecisionGraph](#GuidedDecisionGraph) **Returns**: boolean - is the construction complete ### guidedDecisionGraph.choose(terminal) adds the given terminal to the construction **Kind**: instance method of [GuidedDecisionGraph](#GuidedDecisionGraph) | Param | Type | Description | | --- | --- | --- | | terminal | string | Array.<string> | the name of a terminal vertex in the Decision Graph which is in the current set of possible choices. Or a valid sequence of terminal symbols as an array. | ### guidedDecisionGraph.constructs() ⇒ Array.<string> get a sorted array of possible construction strings from the current state, possibly including nonterminals after the next terminal **Kind**: instance method of [GuidedDecisionGraph](#GuidedDecisionGraph) **Returns**: Array.<string> - a list of possible constructions **Example** ```js // guide is an in-progress GuidedDecisionGraph guide.construction() => ['the', 'dog', 'ate'] guide.choices() => ['', 'the'] guide.constructs() => [ 'the dog ate', 'the dog ate the Noun' 'the dog ate the Noun RelativeClause' ] ``` ### guidedDecisionGraph.pop() ⇒ string pop the last choice off the construction **Kind**: instance method of [GuidedDecisionGraph](#GuidedDecisionGraph) **Returns**: string - the last element of the construction that was submitted through [choose](#GuidedDecisionGraph+choose) **Throws**: - throws an error if called when construction is empty ### guidedDecisionGraph.choices([nDeep]) ⇒ Array.<string> | [Array.<TreeNode>](#TreeNode) returns all possible next terminals, or an array of nDeep [TreeNodes](#TreeNode) **Kind**: instance method of [GuidedDecisionGraph](#GuidedDecisionGraph) **Returns**: Array.<string> | [Array.<TreeNode>](#TreeNode) - if nDeep=1, an array of terminal symbols (strings), else an array of [TreeNodes](#TreeNode) | Param | Type | Default | Description | | --- | --- | --- | --- | | [nDeep] | number | 1 | will search for nDeep possible choices | **Example** ```js // guide is an in-progress GuidedDecisionGraph guide.construction() => ['the', 'dog', 'ate'] guide.choices() => ['', 'the'] guide.choices(3) => [ { val: '', next: [] }, { val: 'the', next: [ { val: 'squirrel', next: [ { val: 'that', next: [] }, { val: '', next: [] } ] }, { val: 'bird', next: [ { val: 'that', next: [] }, { val: '', next: [] } ] }, { val: 'cat', next: [ { val: 'that', next: [] }, { val: '', next: [] } ] }, { val: 'dog', next: [ { val: 'that', next: [] }, { val: '', next: [] } ] } ] } ] ``` ## Recognizer **Kind**: global class * [Recognizer](#Recognizer) * [new Recognizer(dg, start, [seperator])](#new_Recognizer_new) * [.isValid(text)](#Recognizer+isValid) ⇒ boolean * [.isComplete(text)](#Recognizer+isComplete) ⇒ boolean ### new Recognizer(dg, start, [seperator]) create a Recognizer that can test if text is a valid sentence in a grammar | Param | Type | Default | Description | | --- | --- | --- | --- | | dg | [DecisionGraph](#DecisionGraph) | | a Decision Graph that defines a grammar | | start | string | | the name of a vertex in the decision graph from which to start the test | | [seperator] | string | RegExp | "/\\s+/" | how tokens will be divided in given text | ### recognizer.isValid(text) ⇒ boolean is the text a valid in progress sentence in the grammar? Will return true even if the text is not complete. **Kind**: instance method of [Recognizer](#Recognizer) **Returns**: boolean - is the text valid? | Param | Type | Description | | --- | --- | --- | | text | string | the text to check | ### recognizer.isComplete(text) ⇒ boolean is the text a valid and complete text in the grammar? Will return true only if the text is complete. **Kind**: instance method of [Recognizer](#Recognizer) **Returns**: boolean - is the text valid and complete? | Param | Type | Description | | --- | --- | --- | | text | string | the text to check | ## noSelfDefinitions(grammar, returns) check a grammar for direct self-loops **Kind**: global function **Throws**: - an error if one definition of a nonterminal is exactly the nonterminal itself | Param | Type | Description | | --- | --- | --- | | grammar | object | the grammar to check | | returns | true | true if no errors | ## parseGrammar(grammar, [seperator]) ⇒ [DecisionGraph](#DecisionGraph) parse a grammar given as an object and compile it into a decision graph **Kind**: global function **Returns**: [DecisionGraph](#DecisionGraph) - the grammar converted into a decision graph | Param | Type | Default | Description | | --- | --- | --- | --- | | grammar | object | | an object representing a grammar | | [seperator] | string | RegExp | "/\\s+/" | how tokens will be divided in rules | ## reduceGrammar(grammar, [seperator]) ⇒ object reduces the rules of a grammar into a one to one form by assigning a name to all non-terminals. The end result is that each option on a rule with more than one choice will either be a single AND-rule or a single terminal. **Kind**: global function **Returns**: object - the modified grammar object with newly created rules as needed. New rules will be given the name of their parent rule surrounded by underscores and followed by a number. | Param | Type | Default | Description | | --- | --- | --- | --- | | grammar | object | | an object representing a grammar | | [seperator] | string | RegExp | "/\\s+/" | how tokens will be divided in rules | **Example** ```js var grammar = { NounPhrase: ['the Noun', 'the Noun RelativeClause'], RelativeClause: ['that VerbPhrase'], Noun: ['dog', 'cat', 'bird'] } reduceGrammar(grammar) => { NounPhrase: ['_NounPhrase_1', '_NounPhrase_2'], _NounPhrase_1: ['the Noun'], _NounPhrase_2: ['the Noun RelativeClause'], RelativeClause: ['that VerbPhrase'], Noun: ['dog', 'cat', 'bird'] } ``` ## clone(obj) ⇒ object | array helper function to clone a simple object/array made up of primitives. Will not work if the object or array contains non-primitives. **Kind**: global function **Returns**: object | array - a new clone of the provided object or array | Param | Type | Description | | --- | --- | --- | | obj | object | array | an object array made up only of primitives | ## SymbolChain : string a string of one or more symbol names seperated by whitespace or another user defined seperator (see: seperator param for [GrammarGraph](#GrammarGraph)) **Kind**: global typedef **See**: a SymbolChain is used as definitions in [Grammar](#Grammar) **Example** ```js 'dog' // just a single symbol, the word 'dog' 'the Noun RelativeClause' // three symbols ``` ## Grammar : Object a user defined context-free grammar formatted as an object consisting of key-value pairs, with each [non-terminal symbol](https://github.com/jrleszcz/grammar-graph#non-terminal-symbols) pointing to an array of one or more [symbol chains](https://github.com/jrleszcz/grammar-graph#symbol-chains) choices for this non-terminal. **Kind**: global typedef **Properties** | Name | Type | Description | | --- | --- | --- | | symbol | [Array.<SymbolChain>](#SymbolChain) | each element of the array is a possible definition for this symbol. | **Example** ```js var grammar = { Sentence: ['NounPhrase VerbPhrase'], // only one definition of 'Sentence' NounPhrase: ['the Noun', 'the Noun RelativeClause'], // two possible definitions of 'NounPhrase' VerbPhrase: ['Verb', 'Verb NounPhrase'], RelativeClause: ['that VerbPhrase'], Noun: ['dog', 'cat', 'bird', 'squirrel'], // four possible definitions of 'Noun' Verb: ['befriended', 'loved', 'ate', 'attacked'] } // non-terminals: Sentence, NounPhrase, VerbPhrase, RelativeClause, Noun, Verb // terminals: the, that, dog, cat, bird, squirrel, befriended, loved, ate, attacked ``` ## TreeNode : object **Kind**: global typedef **Properties** | Name | Type | Description | | --- | --- | --- | | val | string | a terminal string | | next | [Array.<TreeNode>](#TreeNode) | a list of TreeNodes this node links to |