2.1.1 • Published 7 years ago

tokenary v2.1.1

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

tokenary

Build tokenizers for javascript

Basic Usage (CSV tokenizer)

const {
    tokenary,
    reducer: { ifChar, single, makeToken, everythingUntil }
} = require('tokenary');

const TokenType = {
    comma: 'COMMA',
    value: 'VALUE',
    newline: 'NEWLINE',
};

const tokenizeCSV = tokenary([
    ifChar({
        ',': single(makeToken(TokenType.comma)),
        '\n': single(makeToken(TokenType.newline))
    }),
    consume(everythingUntil(',', '\n'))(makeToken(TokenType.value))
]);

const testCSV = 
`1,Up
2,Left
3,Right`;

const tokens = tokenizeCSV(testCSV);
console.log(prettyPrint(tokens));
/* prints:
[
  <Token type='VALUE' lexeme='1' offset=0>,
  <Token type='COMMA' lexeme=',' offset=1>,
  <Token type='VALUE' lexeme='Up' offset=2>,
  <Token type='NEWLINE' lexeme='\n' offset=4>,
  <Token type='VALUE' lexeme='2' offset=5>,
  <Token type='COMMA' lexeme=',' offset=6>,
  <Token type='VALUE' lexeme='Left' offset=7>,
  <Token type='NEWLINE' lexeme='\n' offset=11>,
  <Token type='VALUE' lexeme='3' offset=12>,
  <Token type='COMMA' lexeme=',' offset=13>,
  <Token type='VALUE' lexeme='Right' offset=14>
]
*/

See examples for more.

API

Classes

Objects

Functions

Typedefs

TokenError

Kind: global class

new TokenError(message, lexeme, offset, state)

ParamTypeDescription
messagestringThe error message
lexemestringThe lexeme this error is for
offsetnumberThe index of the first lexeme character in the text
stateTokStateThe state of the tokenizer when error was created

predicate : object

Functions that return true or false.

Kind: global namespace

predicate.is(truth) ⇒ Predicate

Checks if actual is strict equal (===) to truth

Kind: static method of predicate

ParamType
truthany

predicate.isOneOf(...truths) ⇒ Predicate

Checks if actual is any of the values in truths

Kind: static method of predicate

ParamType
...truthsany

predicate.matches(regex) ⇒ Predicate

Checks if actual matches the regular expression

Kind: static method of predicate

ParamType
regexRegExp

predicate.not(predicate) ⇒ Predicate

Logical not operator on a predicate

Kind: static method of predicate

ParamType
predicatePredicate

predicate.or(...predicates) ⇒ Predicate

Logical or operator on predicates

Kind: static method of predicate

ParamType
...predicatesPredicate

predicate.nor(...predicates) ⇒ Predicate

Logical nor operator on predicates. Short for not(or(...predicates))

Kind: static method of predicate

ParamType
...predicatesPredicate

predicate.and(...predicates) ⇒ Predicate

Logical and operator on predicates

Kind: static method of predicate

ParamType
...predicatesPredicate

predicate.nand(...predicates) ⇒ Predicate

Logical nand operator on predicates. Short for not(and(...predicates))

Kind: static method of predicate

ParamType
...predicatesPredicate

predicate.xor(predicate1, predicate2) ⇒ Predicate

Logical xor operator on 2 predicates

Kind: static method of predicate

ParamType
predicate1Predicate
predicate2Predicate

predicate.Predicate ⇒ boolean

Kind: static typedef of predicate

ParamType
actualany

reducer : object

Kind: global namespace

reducer.keywords(keywordMap, settings) ⇒ Reducer

Extracts keywords from the text

Kind: static method of reducer

ParamTypeDescription
keywordMapObject.<string, TokenCreator>Map of keywords to check for
settingsobject
settings.charsetRegExpCharset allowed for a keyword
settings.firstCharRegExpCharset allowed for first character of keyword
settings.noMatchTokenCreatorToken creator to use on invalid keywords

reducer.ifThen(predicate) ⇒ function

If the predicate is true, run the reducer

Kind: static method of reducer

ParamTypeDescription
predicatePredicateCondition to be met

reducer.ifChar(reducerMap) ⇒ Reducer

If a matching character is found, runs the given reducer

Kind: static method of reducer

ParamTypeDescription
reducerMapObject.<string, Reducer>character to reducer map to check

reducer.single(tokenCreator) ⇒ Reducer

Creates a token from the single current character

Kind: static method of reducer

ParamType
tokenCreatorTokenCreator

reducer.consume(reducer) ⇒ function

Creates a token from the characters passed over in the given reducer Expects the given reducers to not return any tokens

Kind: static method of reducer

ParamType
reducerReducer

reducer.sequence(reducers) ⇒ Reducer

Pipe output of a sequence of reducers together (left to right)

Kind: static method of reducer

ParamType
reducersArray.<Reducer>

reducer.everything() ⇒ Reducer

Creates a token from every character that follows

Kind: static method of reducer

reducer.everythingUntil(chars) ⇒ Reducer

Creates a token from every character that follows until one given is reached

Kind: static method of reducer

ParamTypeDescription
charsArray.<string>Characters to possibly match

reducer.char(char) ⇒ Reducer

Advances past a single character, throws an error if it's not what is expected

Kind: static method of reducer

ParamTypeDescription
charstringExpected character

reducer.untilRegexFails(regex) ⇒ Reducer

Runs the regex until the regex fails on all characters advanced past

Kind: static method of reducer

ParamType
regexRegExp

reducer.whitespace() ⇒ Reducer

Advances past all contiguous whitespace characters

Kind: static method of reducer

token : object

Kind: global namespace

token.makeToken(type) ⇒ TokenCreator

Creates a token object

Kind: static method of token

ParamTypeDescription
typestringType name of token

token.makeNothing() : TokenCreator

Creates nothing

Kind: static method of token

token.makeError(message) ⇒ TokenCreator

Throws an error

Kind: static method of token
Throws:

  • TokenError
ParamType
messagestring

token.stringifyToken(token) ⇒ string

Formats a token for printing

Kind: static method of token

ParamTypeDescription
tokenTokenToken to stringify

token.prettyPrint(tokens) ⇒ string

Formats an array of tokens for printing

Kind: static method of token

ParamTypeDescription
tokensArray.<Token>Tokens to pretty print

tokenary(reducers, settings) ⇒ function

Creates a tokenizing function

Kind: global function

ParamTypeDescription
reducersArray.<Reducer>Main reducers
settingsobjectSettings for tokenizer
settings.catcherTokenCreatorToken creator to use if an error is found

TokState : reducer.Reducer

Kind: global typedef

TokState.create(text, current, tokens) ⇒ TokState

Creates a TokState object

Kind: static method of TokState

ParamTypeDefaultDescription
textstringThe text represented by the state
currentnumber0Current offset in text
tokensArray.<Token>Tokens created so far

TokState.advance(state) ⇒ TokState

Increments TokState.current by 1 (creates new object)

Kind: static method of TokState

ParamType
stateTokState

TokenCreator : tokState.TokState

Kind: global typedef

Reducer ⇒ TokState

Kind: global typedef

ParamTypeDescription
stateTokStateThe state to modify

TokState : tokState.TokState

Kind: global typedef

TokState.create(text, current, tokens) ⇒ TokState

Creates a TokState object

Kind: static method of TokState

ParamTypeDefaultDescription
textstringThe text represented by the state
currentnumber0Current offset in text
tokensArray.<Token>Tokens created so far

TokState.advance(state) ⇒ TokState

Increments TokState.current by 1 (creates new object)

Kind: static method of TokState

ParamType
stateTokState

Token

Kind: global typedef
Properties

NameTypeDescription
typestringType of token
lexemestringThe text this token represents
offsetnumberThe offset of the first character of the lexeme

TokenCreator ⇒ Token | undefined

Kind: global typedef

ParamTypeDescription
lexemestringThe text this token represents
offsetnumberThe offset of the first character of the lexeme
stateTokStateState of tokenizer when created (optional)

Token : token.Token

Kind: global typedef

TokState

Kind: global typedef
Properties

NameTypeDescription
textstringThe text represented by the state
currentnumberCurrent offset in text
tokensArray.<Token>Tokens created so far

TokState.create(text, current, tokens) ⇒ TokState

Creates a TokState object

Kind: static method of TokState

ParamTypeDefaultDescription
textstringThe text represented by the state
currentnumber0Current offset in text
tokensArray.<Token>Tokens created so far

TokState.advance(state) ⇒ TokState

Increments TokState.current by 1 (creates new object)

Kind: static method of TokState

ParamType
stateTokState

TokState : tokState.TokState

Kind: global typedef

TokState.create(text, current, tokens) ⇒ TokState

Creates a TokState object

Kind: static method of TokState

ParamTypeDefaultDescription
textstringThe text represented by the state
currentnumber0Current offset in text
tokensArray.<Token>Tokens created so far

TokState.advance(state) ⇒ TokState

Increments TokState.current by 1 (creates new object)

Kind: static method of TokState

ParamType
stateTokState
2.1.1

7 years ago

2.1.0

7 years ago

2.0.0

7 years ago

1.1.1

7 years ago

1.1.0

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago

0.0.1

7 years ago