Giter Club home page Giter Club logo

gvm's Introduction

GVM Build Status

This project contains my experimental project with extendable syntax grammar.

Brief

Core of project is syntax grammar (gvm.language.Grammar).

General elements of it's:

  • gvm.language.TokenID - token identifier, e.g. terminal symbol
  • gvm.language.ParseletID - parselet identifier, e.g. non-terminal symbol

Grammar also contains patterns for tokenize from source text to syntax tokens (gvm.language.SyntaxToken).

Example of grammar

This version is used explicit definition of grammar (e.g. parselet definition is imperative)

from gvm.language.combinators import make_sequence
from gvm.language.grammar import Grammar, ParseletKind

grammar = Grammar()

grammar.add_pattern(grammar.add_token('Whitespace'), r'\s+')
grammar.add_pattern(grammar.add_token('Name'), r'[a-zA-Z_][a-zA-Z0-9]*')
number_id = grammar.add_pattern(grammar.add_token('Number'), r'[0-9]+')
expr_id = grammar.add_parselet('expr', kind=ParseletKind.Pratt)
grammar.add_trivia(grammar.tokens['Whitespace'])

implicit = grammar.add_implicit

# expr := Number
grammar.add_parser(expr_id, number_id)
# expr := expr '+' expr
grammar.add_parser(expr_id, make_sequence(expr_id, implicit('+'), expr_id), priority=100)
# expr := expr '-' expr
grammar.add_parser(expr_id, make_sequence(expr_id, implicit('-'), expr_id), priority=100)
# expr := expr '*' expr
grammar.add_parser(expr_id, make_sequence(expr_id, implicit('*'), expr_id), priority=200)
# expr := expr '/' expr
grammar.add_parser(expr_id, make_sequence(expr_id, implicit('/'), expr_id), priority=200)
# expr := '-' expr
grammar.add_parser(expr_id, make_sequence(implicit('-'), expr_id))
# expr := '-' expr
grammar.add_parser(expr_id, make_sequence(implicit('+'), expr_id))
# expr := '(' expr ')'
grammar.add_parser(expr_id, make_sequence(implicit('('), expr_id, implicit(')')))

License

This project is published by MIT license. For full test of license view LICENSE.md

Installation

This project is used Python 3.7 as minimum version.

Also install attrs and pytest (for tests)

Testing

Run pytest

gvm's People

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.