10.0.2 • Published 22 days ago

pratt-parser v10.0.2

Weekly downloads
538
License
BSD-2-Clause
Repository
github
Last release
22 days ago

npm License Typed with TypeScript bundlejs downloads GitHub Issues Build Status Styled with prettier Commitizen friendly Known Vulnerabilities Coverage Status

pratt-parser

Pratt Parser

Based on Top Down Operator Precedence and Douglas Crockford TDOP

import { Parser, WhiteSpaceToken, NumberToken } from "pratt-parser";

function Value(value) {
  return Object.create(null, {
    value: {
      value: value
    }
  });
}

const myGrammar = new Parser({
  tokens: [WhiteSpaceToken, NumberToken],
  prefix: {
    "(": {
      nud(grammar) {
        const e = grammar.expression(0);
        grammar.advance(")");
        return e;
      }
    }
  },
  infix: {
    ")": {},
    "+": {
      precedence: 50,
      combine: (left, right) => Value(left.value + right.value)
    },
    "-": {
      precedence: 50,
      combine: (left, right) => Value(left.value - right.value)
    },
    "*": {
      precedence: 60,
      combine: (left, right) => Value(left.value * right.value)
    },
    "/": {
      precedence: 60,
      combine: (left, right) => Value(left.value / right.value)
    }
  }
});

console.log(myGrammar.parse("(1 + (1 + 4 * 3)) * (2 + 1)").value);

API

Table of Contents

ParsePosition

Type: Object

Properties

Token

Type: Object

Properties

RootToken

Base object for all tokens

parseString

Parses from chunk of PrasePosition and delivers next token Modifies ParsePosition so that it points behind the detected token.

Parameters

Returns Token

WhiteSpaceToken

skip white space

LineCommentToken

skips until end of line

EOFToken

Token representing 'end of file'

Parser

Creates a grammar for later parsing

Parameters

error

Forwards error to the tokenizer

Parameters

  • args ...any

Returns Object error

parse

Parses the input and delivers the outermoost expression.

Parameters

  • chunk string input text
  • context Object object transparently passed to tokenizer

Returns Object evaluated input

Tokenizer

Creates a tokenizer for later parsing.

Parameters

  • grammar Object definition of the grammar with operators...

tokens

delivers tokens from the input.

Parameters

  • chunk string the input to be processed
  • context Object additional info to be used by the actual token types

error

Parameters

Returns Object error

install

With npm do:

npm install pratt-parser

license

BSD-2-Clause

10.0.1

22 days ago

10.0.2

22 days ago

10.0.0

2 months ago

9.0.2

11 months ago

9.0.1

11 months ago

9.0.0

1 year ago

8.1.0

2 years ago

8.0.19

2 years ago

8.0.21

2 years ago

8.0.20

2 years ago

8.0.18

2 years ago

8.0.16

2 years ago

8.0.15

2 years ago

8.0.17

2 years ago

8.0.14

3 years ago

8.0.13

3 years ago

8.0.12

3 years ago

8.0.11

3 years ago

8.0.10

3 years ago

8.0.9

4 years ago

8.0.8

4 years ago

8.0.7

4 years ago

8.0.6

4 years ago

8.0.5

4 years ago

8.0.4

4 years ago

8.0.3

4 years ago

8.0.2

4 years ago

8.0.1

5 years ago

8.0.0

5 years ago

7.0.9

6 years ago

7.0.8

6 years ago

7.0.7

7 years ago

7.0.6

7 years ago

7.0.5

7 years ago

7.0.4

7 years ago

7.0.3

7 years ago

7.0.2

7 years ago

7.0.1

7 years ago

7.0.0

7 years ago

6.1.0

7 years ago

6.0.1

7 years ago

6.0.0

7 years ago

5.0.1

7 years ago

5.0.0

7 years ago

4.1.0

7 years ago

4.0.0

7 years ago

3.1.3

7 years ago

3.1.2

7 years ago

3.1.1

7 years ago

3.1.0

7 years ago

3.0.4

7 years ago

3.0.3

7 years ago

3.0.2

7 years ago

3.0.1

7 years ago

3.0.0

7 years ago

2.0.18

7 years ago

2.0.17

7 years ago

2.0.16

7 years ago

2.0.15

7 years ago

2.0.14

7 years ago

2.0.13

7 years ago

2.0.12

7 years ago

2.0.11

7 years ago

2.0.10

7 years ago

2.0.9

7 years ago

2.0.8

7 years ago

2.0.7

7 years ago

2.0.6

7 years ago

2.0.5

7 years ago

2.0.4

8 years ago

2.0.3

8 years ago

2.0.2

8 years ago

2.0.1

8 years ago

2.0.0

8 years ago

1.5.4

8 years ago

1.5.3

8 years ago

1.5.2

8 years ago

1.5.1

8 years ago

1.5.0

8 years ago

1.4.3

8 years ago

1.4.2

8 years ago

1.4.1

8 years ago

1.3.1

8 years ago

1.3.0

8 years ago

1.2.1

8 years ago

1.2.0

8 years ago

1.1.0

8 years ago

1.0.2

8 years ago

1.0.1

8 years ago

1.0.0

9 years ago