Code Monkey home page Code Monkey logo

tokenizer's Introduction

Tokenizer

How to use the tokenizer:

Import initTokenizer from the app.js file. You can then get a tokenizer object by calling initTokenizer and provide it with two arguments:

  1. An array of objects containing the token type and a regular expression. The objects should be formatted like this:
const grammarObject = {
  tokenType: 'NUMBER',
  tokenRegExp: /^.*[0-9]/
}
  1. A string you wish to analyze for matching tokens.

You can then call the methods below on your Tokenizer-object.

Public interface

Methods

currentActiveToken Returns the current active token. The last token is always END.

moveToNextToken() Moves active token one step forward. Does not return a token. You can not move past the END token.

moveToPreviousToken() Moves active token one step backwards. Does not return a token. You can not move past the first token.

Examples

import { initTokenizer } from '<filepath>'

const grammars = [{
  tokenType: 'WORD',
  tokenRegExp: /^[\w|åäöÅÄÖ]+/
}, {
  tokenType: 'DOT',
  tokenRegExp: /\./
}]

const tokenizer = initTokenizer(grammars, 'Hello world.')

// To get the active token:

const activeToken = tokenizer.currentActiveToken

// If you wish to get the next token:
tokenizer.moveToNextToken()
const nextActiveToken = tokenizer.currentActiveToken()

// If you wish to get the previous token:
tokenizer.moveToPreviousToken()
const nextActiveToken = tokenizer.currentActiveToken()

tokenizer's People

Contributors

dntoll avatar

Watchers

Rebecca A avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.