Skip to main content

Grammar compilation for Caustic

Project description

Caustic's lexing/grammar framework

The basic_compiler module is a less advanced compiler, but is used to bootstrap the compiler module

The compiler module compiles grammars from Caustic grammar (.cag) files into nodes, and uses a grammer system built in Caustic grammar format and compiled with the basic_compiler module

The nodes module provides the nodes themselves, and allows manually building grammar by supplying nodes

The serialize module provides functions for serializing and deserializing nodes

The util module provides small utilities

The .cag specification

Pragmas

Pragmas are special directives embedded in the grammar
These are only supported on the bootstrapped compile module

Include / Import

$include [path], $import [path]

Allows putting multiple grammar files together

Relative paths provided as [path] will be checked against the following directories, in order:

  • The path of the includer/importer (if possible)
  • The builtin_path of the compiler module (the location of compiler.py)
  • The current directory

The difference between include and import is that included nodes will be bound with the rest of the nodes from the includer, whilst imported nodes will not

Comments

Comments may start with a #

Statements

A statement begins with an identifier, followed by an =, then an expression, and finally a ;

Identifier

An identifier is a sequence of alphanumeric characters and underscores

Expression

Expressions consist of nodes, where a node can be as simple as a string to as complex as a group

Naming

nodes.Node.name

Named nodes are denoted by a name (alphanumeric and underscores), followed by a :, and then the node/expression
This controls the return value of containing groups

Anonymous

"Anonymous" named nodes are expressions prefixed with :, but with no leading name

Group

nodes.NodeGroup

The top level of an expression is implicitly grouped

A simple group node is opened by ( and closed by )
Groups match the nodes inside of them in a sequence in order
The return value of this group will be dependent on its contents' naming:

  • A group containing no named nodes will return a list of its nodes' results
  • A group containing nodes with "anonymous" names returns the last matched anonymous nodes' return value
  • A group containing named nodes returns a dict containing a mapping of the names to the nodes' results

Mixing anonymous and named expressions in a single group will result in an error

Whitespace sensitive group

nodes.NodeGroup, keep_whitespace=True

A whitespace sensitive group is opened by { and closed by }
The only difference between this type of group and a normal group is that it does not implicitly discard whitespace between its nodes

Union

nodes.UnionNode

A union is opened by [ and closed by ]
Unions match any of their contained nodes

Range

nodes.NodeRange

Can be created in the following ways:

  • - [node]: Matches any amount of [node]
  • x- [node]: Matches x or more of [node]
  • -x [node]: Matches up to (but not including) x of [node]
  • a-b [node]: Matches between a (inclusive) and b (exclusive) of [node]

Note that this should be placed after a (name)[#naming]

Real

Real nodes are nodes that actually match content, such as strings or patterns

String

nodes.StringNode

The simplest node, denoted either by single quotes ('') or double quotes ("")
Supports escape characters

Note: despite the name of this node, it is important to remember that the nodes only match bytes!

Pattern

nodes.PatternNode

Matches a regular expression, denoted by slashes (/) in the following syntax:

target group / pattern / flags

Target Group

In a pattern, if a target group is given (as an integer), the result of this node will be the bytes of that group instead of the entire match

Flags

Supports these common RegEx flags:

  • i: ignore case / case insensitive
  • m: multiline - ^ matches beginning of line or string, $ matches end of either
  • s: single-line / "dotall" - . matches newlines as well

Meta

"Meta" nodes that don't actually match anything, but can change some context

Stealer

nodes.Stealer

A "stealer" node is denoted by a !, and is only acceptable in a group

If a group reaches a "stealer" node, then the group will raise an exception if any of the subsequent nodes fail

Context

nodes.Context

A context is created with an opening < and closing >
Context nodes always mach, with the result being the (string) contents

Context nodes should contain either a string, or a short sequence of alphanumeric characters and underscores

Node Reference

nodes.NodeRef

Denoted by an @, followed by a node name (as a string of alphanumeric characters and underscore)

Matches the value of the targeted node, and returns the result of that

Must be bound using either its .bind() method, or automatically through the default compilers

Changelog

0.2.0

  • Implemented node saving and loading through the serialize module
  • Moved compiler.bind_nodes() to util.bind_nodes()

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

caustic_lexer-0.2.tar.gz (19.4 kB view details)

Uploaded Source

Built Distribution

caustic.lexer-0.2.0-py3-none-any.whl (20.9 kB view details)

Uploaded Python 3

File details

Details for the file caustic_lexer-0.2.tar.gz.

File metadata

  • Download URL: caustic_lexer-0.2.tar.gz
  • Upload date:
  • Size: 19.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.8

File hashes

Hashes for caustic_lexer-0.2.tar.gz
Algorithm Hash digest
SHA256 7ad259aecc9a85175ab1e6ec76ef3a1f5549efde7950ec47047e040f2134c350
MD5 ed0176c15dda47970cab9c499c3a60c9
BLAKE2b-256 c53e2841d10d4eeafc22c8a7eff328774f3496c65a55f12a36445a97ccbd5813

See more details on using hashes here.

File details

Details for the file caustic.lexer-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for caustic.lexer-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 695a57f8be9a8fd650bde2fb34bdf5cbcfaceea1f7b870acec63e9acc44ddcff
MD5 a959cc58d73152e5dccf46099ec588f9
BLAKE2b-256 63184f758354192b49fd13c55610ab156b754282d2f393ef4abc01781ce3d19c

See more details on using hashes here.

Supported by

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