Skip to main content

A lexer creation tool

Project description

Soliz - A lexer creation tool

Features

  • Provides basic implementations for: strings, integers, floats, operators, symbols
  • Soliz's abstract design allows quick and easily understandable development
  • Pretty error printing

Errors look like:

Error: Unexpected character
{"key": qas}
        ^
Occurs at: 1:9-9

WARNING: Order of the rules may matter depending on the implementation.

Examples

JSON

from typing import Tuple

from src.soliz.error import Error
from src.soliz import StringRule, NumberRule
from src.soliz.lex import Lexer, Rule, Context
from src.soliz import Token


class TokenType:
    TT_LBRACKET = "LBRACKET"
    TT_RBRACKET = "RBRACKET"
    TT_COLON = "COLON"


class JSONRules(Rule):
    def check(self, ctx: Context) -> Tuple[Token, bool] | None:
        match ctx.char:
            case '{':
                return Token(TokenType.TT_LBRACKET, ctx.span()), True
            case '}':
                return Token(TokenType.TT_RBRACKET, ctx.span()), True
            case ':':
                return Token(TokenType.TT_COLON, ctx.span()), True


def main() -> None:
    lexer = Lexer([JSONRules(), StringRule(), NumberRule()])
    text = input("Enter JSON: ")

    try:
        tokens = lexer.lex(text)
        print(tokens)
    except Error as err:
        err.pretty_print(text)

Builtins

Builtin rules can be located in soliz/impls.py

StringRule

This rule allows escape sequences and parses quoted strings.
Errors on invalid escape sequences.

NOTE: This rule allows newlines inside the string.

NumberRule

This rule parses integers and floats, positive or negative.

OperatorRule

Parses the following operators: +, -, *, /, ==, ^, **, %

SymbolRule

Parses the following symbols: (, ), =, . as individual token types without value.

IdentifierRule

Parses identifiers that start with _ or alphabetic characters, and continue with alphanumeric or _ characters.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

soliz-0.0.1.tar.gz (7.3 kB view details)

Uploaded Source

File details

Details for the file soliz-0.0.1.tar.gz.

File metadata

  • Download URL: soliz-0.0.1.tar.gz
  • Upload date:
  • Size: 7.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.12.0

File hashes

Hashes for soliz-0.0.1.tar.gz
Algorithm Hash digest
SHA256 e4e85d85601834140e938581df493ec880995416e2eae2e1d060fe79b8e38851
MD5 ffe69377e67fdb8733919a943a9ead65
BLAKE2b-256 258a52ca715dbf1c80631fed2fe47231b40df31643926716606404976b3abb92

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page