Giter VIP home page Giter VIP logo

jly-lexer's Introduction

Usage

type dict = {
    macros?: {
        [macrosName: string]: string // a macro is allowed to contain any number of other macros
    };
    rules: {
        r: string; // a regular expression after escaped, use `{one_macro}` to insert a macro
        name: string;
        func?: (t: (undefined | { yytext: string; lexer: any })) => (string | undefined);
        // if return undefined, value of `name` will be the type of token for this rule
    }[];
    tokens: (string[]) | string // if tokens is string, the finally tokens will be tokens.split(/ +/)
}
// the token types from `rules` must be included in `tokens`
// if there are mutiple rules matching at the same time, the last defined rule will be adopted

example:

let dict = {
    macros: {
        "int": "-?(?:[0-9]|[1-9][0-9]+)",
        "exp": "(?:[eE][-+]?[0-9]+)",
        "frac": "(?:\\.[0-9]+)"
    },
    rules: [{
        r: "{int}{frac}?{exp}?\\b",
        name: "number",
        func: function (t) {
            return 'NUMBER';
        }
    }, {
        r: "\\n", name: "new_line", func: function (t) {
            t.lexer.lineno++;
        }
    }, {
        r: "[ \\t\\r\\a]+", name: "skip", // if `name` is skip, the text that match this rule will be ignored
    },],
    tokens: "NUMBER"
};
let input = `123.6e-12
4   5
10e12
`;
let lexer = new Lexer(dict, input);
while (true) {
    let type = lexer.lex();
    if (type === "EOF") break;
    console.log(type, lexer.yytext);
}
/*
output:
NUMBER 123.6e-12
NUMBER 4
NUMBER 5
NUMBER 10e12
*/

jly-lexer's People

Contributors

yyuneko avatar

Watchers

 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.