1.3.2 • Published 3 years ago

@roku-road/bright v1.3.2

Weekly downloads
339
License
MIT
Repository
github
Last release
3 years ago

Bright

npm version Downloads/month Build Status CircleCI Coverage Status Dependency Status Greenkeeper badge CodeFactor

What is Bright?

Blazing fast parser for BrightScript that gives you ESTree like AST. Sort of. The motivation behind this project is to build solid platform and base for other development tools in Roku world. This parser takes .brs files and builds AST in form of ESTree like structure that could be used with ESLint, Prettier or other tools for linting, refactoring and formatting.

This project uses awesome https://github.com/SAP/chevrotain parser engine, performance tests : https://sap.github.io/chevrotain/performance/

Performance

While there is no official metrics yet, during development it continuesly got tested on ~800 random open source project files from Github.

Tokei

Thanks, Tokei

Tests

Project is written in TypeScript and compiles to JS

Installation

yarn

Usage

import { ast, parse } from '@roku-road/bright'

export const scanSource = (source: string, type = 'Program') => {
  const { value, lexErrors, tokens, parseErrors } = parse(source, type)

  //...

  return ast(value)
}

Example

Library "ads"

Will produce

Tokens

 [ { loc: { start: { column: 1, line: 1 }, end: { column: 7, line: 1 } },
           range: [ 0, 6 ],
           type: 'LIBRARY',
           value: 'Library' },
         { loc: { start: { column: 9, line: 1 }, end: { column: 13, line: 1 } },
           range: [ 8, 12 ],
           type: 'STRING_LITERAL',
           value: '"ads"' } ]

and value

    { value:
       { name: 'Program',
         children:
          { Declaration:
             [ { name: 'LibraryStatement',
                 children:
                  { LIBRARY:
                     [ { image: 'Library',
                         startOffset: 0,
                         endOffset: 6,
                         startLine: 1,
                         endLine: 1,
                         startColumn: 1,
                         endColumn: 7,
                         tokenTypeIdx: 39 } ],
                    path:
                     [ { image: '"ads"',
                         startOffset: 8,
                         endOffset: 12,
                         startLine: 1,
                         endLine: 1,
                         startColumn: 9,
                         endColumn: 13,
                         tokenTypeIdx: 79
                           ],
                            tokenTypeIdx: 79,
                            categoryMatches: [],
                            categoryMatchesMap: {},
                            tokenName: 'STRING_LITERAL',
                            isParent: false } } ] } } ],
            EOF:
             [ { image: '',
                 startOffset: NaN,
                 endOffset: NaN,
                 startLine: NaN,
                 endLine: NaN,
                 startColumn: NaN,
                 endColumn: NaN,
                 tokenTypeIdx: 1,
                 } ] } } }

Errors

Lets say we forget to put a new line after function signature declaration

function a end function
[ Error {
           name: 'MismatchedTokenException',
           message: 'Expecting token of type --> TERMINATOR <-- but found --> \'end function\' <--',
           token:
            { image: 'end function',
              startOffset: 11,
              endOffset: 22,
              startLine: 1,
              endLine: 1,
              startColumn: 12,
              endColumn: 23,
              tokenTypeIdx: 29,
              tokenType:
               { PATTERN: [Function: pattern],
                 tokenTypeIdx: 29,
                 CATEGORIES: [],
                 categoryMatches: [],
                 categoryMatchesMap: {},
                 tokenName: 'END_FUNCTION',
                 isParent: false,
                 LONGER_ALT:
                  { PATTERN: /([A-Za-z_]+[A-Za-z0-9_]*)/,
                    tokenTypeIdx: 3,
                    CATEGORIES: [],
                    categoryMatches: [],
                    categoryMatchesMap: {},
                    tokenName: 'IDENTIFIER',
                    isParent: false },
                 START_CHARS_HINT: [ 'E', 'e' ] } },
           resyncedTokens: [],
           context:
            { ruleStack: [ 'Program', 'FunctionDeclaration', 'EndOfStatement' ],
              ruleOccurrenceStack: [ 0, 0, 0 ] } } ]
Rendered as
    > 1 | function a end function
        |            ^^^^^^^^^^^ Expecting token of type --> TERMINATOR <-- but found --> 'end function' <--

Bright consists of Tokens, Parser and Visitors. Please see Chevrotain project for details

ElementDescription
RokuBRSParserHeart of the project, defined structure of nodes
ASTVisitorVisitor that walks parsed CST and produce AST for other tools
ALL_TOKENSMap of the tokens, literals, punctuation etc
parseAPI function to get parsed value, lexer and parser errors
astAPI function to get AST tree value, lexer and parser errors
visitorKeysMap for walking though the tree (to avoid blind iteration)

Grammar

Please check generated https://github.com/RokuRoad/bright/blob/master/diagram/index.html for details

1.3.2

3 years ago

1.3.1

3 years ago

1.3.0

3 years ago

1.2.12

4 years ago

1.2.13

4 years ago

1.2.11

6 years ago

1.2.10

6 years ago

1.2.9

6 years ago

1.2.8

6 years ago

1.2.7

6 years ago

1.2.6

6 years ago

1.2.5

6 years ago

1.2.4

6 years ago

1.2.3

6 years ago

1.2.2

6 years ago

1.2.1

6 years ago

1.2.0

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago

0.5.5

6 years ago

0.5.3

6 years ago

0.5.2

6 years ago

0.5.1

6 years ago

0.5.0

6 years ago