Skip to main content

HYAML is a one-liner oriented language

Project description


HYAML is an expression-oriented language which serves the purpose of writing consice config files without introducing all the power of a fully-featured scripting language. In a nutshell, it's an extensible DSL for writing configs.

The syntax is inspired by the Python programming language. At the same time, it lacks a lot of features Python has since they're not relevant to the common configuration tasks. For example, you won't see list expressions or lambdas here.

Initially, HYAML was built with the library named [CodeTalker]( That lib got the job done, however, its main purpose was to be as fast as possible. To be the fastest language parser around CodeTalker used Cython underneath that is essentially C with Python-like syntax. Since Cython, just as C, is a compiled language, it compiles CodeTalker's sources on installation. As time went by, this became a major pain point for maintenance. In addition to that, HYAML wasn't meant to be _that_ fast because of the way it's used: after reading a config file all HYAML-expressions are translated to regular Python functions. This is done once on startup and it's usually _fast enough_. Eventually, HYAML's backend was switched to [ANTLR](

## Installation

Use pip to install HYAML. It's tested against Python 3.7 and work with earlier versions is not guaranteed.

pip install hyaml

### Work-in-progress version

To install a version that hasn't yet been published to the main index run the following command:

pip install --index-url --extra-index-url hyaml

## Language

HYAML is an expression-oriented language or, to put it another way, it's a one-liner-oriented language. It doesn't support statements or things like assignments (directly, though it can be enhanced in certain ways).

## Features

### Primitives

The exhaustive list of supported primitive values:

- Numbers, including integers and floats (use regular notation, as in `1`, `-1`, `1.0`).
- Strings, enclosed by single or double quotes (`'foo'`, `"bar"`).
- Boolean values are represented as `true` and `false`.
- Variables (`$var_name`).

### Basic math

Addition, subtraction, multiplication, and division are backed by Python. Pay special attention to division since it's translated to `/` which is float point division rather than integer one (namely, `//`). It works like this for historical reasons only.

1 + 5 # addition
-3--5 # subtraction, translated to "-3 - -5", use if you don't like spaces
5 * 2 # multiplication
10 / 2 # division

### Logical operators

Logical operators are `and`, `or`, and `not`, just as in Python:

$a and $b # conjunction
$b or $c # disjunction
not $a # negation
$a and ($b or $c) # grouping
$foo and not false # negation precedes other operations, conjunction precedes disjunction

### Comparsison

Again, as in Python and most modern programming languages

1 > 2 # > stands for greater
1 >= 2 # greater or equal
1 < 2 # less
1 <= 2 # less or equal
1 == 2 # equal
1 != 2 # not equal

Range comparisons are supported :tada:

10 > $a > 5 # checks whether $a less than 10 and greater than 5

### Lists and dicts

Compared to the previous version of HYAML lists (aka arrays) and dicts (aka associative arrays, maps, and hashmaps) are first-level citizens :tada::

[1, 2, 3] + [$x, $y, $z] # concatenation of two lists
{abc: 123} # creates a dict in python {"abc: 123"}

Note that dict keys don't require quotes and support hyphens and colons as non-terminating symbols. In other words, `{Sub-Attr:1: 500}` is translated to `{"Sub-Attr:1": 500}`.

### Accessing attributes

A common task when working with HYAML is accessing nested values in a dict. This is what `.` does:


The expression above corresponds to the following Python code


`-`, `_`, `:` are special characters allowed for attribute names. When a dict doesn't have the key requested, it will produce a runtime error.

### Calling methods

Values in HYAML can have methods. Essentially, they are not methods at all but regular functions. There's nothing special about method calls except for adding parentheses is required even if the method doesn't take arguments.


Methods returning booleans usually end with `?`:


### Work around null

`null` value (known in Python as `None`) is a common source of errors when the value you're working with is not set. It's a burden for both static (such as C or C++) and dynamic (such as Python or JavaScript) languages. Nowadays, many of them offer a concise form of checks for value presence (null-checks). HYAML follows the trend and has support for the [safe navigation operator](


In the example above, if `$dict_var` is `None` or doesn't contain `Key-Name:1` this won't produce an error. Of course, you can have chains when accessing nested attributes:

$dict_var?.Key?.Nested-Key # this is safe now, yay!

The operators works for methods as well:

$dict_var?.substring(4)?.size() or 0

## API

### Translator API

Translator takes a (presumably) valid string HYAML and generates a (most likely) valid Python expression:

from hyaml.translator import translate

# get(variables, 'foo')
# is_odd(1)

### Compiler API

Compiler takes one step further and makes a function from the given expression:

from hyaml.compiler import compile

nine_plus_five = compiler("9 + 5")
# 9

For using variables in expressions you'll need to add bindings to functions:

from hyaml.compiler import Compiler
compile = Compiler(bindings=("variables",))

inc = compile("$var + 1")
inc({"var": 1})
# 2
inc({"var": 10})
# 11

In order to work with methods you'll need to provide a table of functions to the compiler:

from hyaml.compiler import Compiler

methods = {
"square": lambda x: x ** 2,
"is_like": lambda x, y: x.startswith(y)
compile = Compiler(method_name=methods)

square = compile("5.square()")
# 25
like = compiler("'abcdef'.like?('ab')")
# True

Finally, using variables and methods together:

from hyaml.compiler import Compiler

methods = {
"square": lambda x: x ** 2,
"is_like": lambda x, y: x.startswith(y)
compile = Compiler(method_name=methods, bindings=("variables",))
square = compile("$x.square()")
square({"x": 10})
# 100
square({"x": 7})
# 49
like = compile("'abcdef'.like?($str)")
like({"str": "ab"})
# True
like({"str": "abc"})
# True

For those who curious, there's a module named "prelude" which holds globally available methods. They are used for implementing some language features. Specifically, the subscription operator (aka square brackets) relies on `get`, safe navigation to attributes and method calls relies on `safe_get` and `safe_call` respectively. You can override those methods with your own variants using `method_table` (but you cannot remove them, why would you anyway?).

## Running tests

python -m unittest tests/test_*

## Playground

HYAML is provided with interactive shell for testing out the language, fire it up with `hyaml`

$ hyaml

>>> evaluate("$x.starts_with?('foo')", x="foobar")


TL;DR: ANTLR is a tool for building language parsers. Be sure you visited its [website]( and checked out the [source code]( ANTLR is written in Java but can generate parsers in Python. Compared to CodeTalker it has plenty of [examples]( and a ton of answered question at StackOverflow.

In order to build a new parser, follow the instructions (taken from the ANTLR's github [page](

- Install ANTLR from
- Define your language grammar in the .g4 format (there's an extension for VSCode!).
- Generate the output by running
antlr4 -Dlanguage=Python3 MyGrammar.g4
- Add all generated/updated files to commit.
- You're all set!

## G4

One can the documentation or even read a book about ANTLR but building a new language can be done by examples. At least, this is the way Hyaml.g4 was written.

## Lexer/Parser API

A simple example of using generated lexer and parser follows:

import sys
from antlr4 import *
from HyamlLexer import HyamlLexer
from HyamlParser import HyamlParser

def main(argv):
input = FileStream(argv[1])
### Create a lexer instance and pass an IO object to its constructor
lexer = HyamlLexer(input)
stream = CommonTokenStream(lexer)
### Create a parser and feed it with tokens
parser = HyamlParser(stream)
tree = parser.prog()
lisp_tree_str = tree.toStringTree(recog=parser)
### Print a simple tree representation

if __name__ == '__main__':

Running this script with `python sample.txt` prints something like:
(prog (expr (expr $cdr) (callChain (link (methodCall . some (arguments ( (exprList (expr (listLiteral [ ]))) )))))) \n)

For transforming parsed structures you'll need a listener.

## Listener API

Along with a lexer and a parser, ANTLR generates a listener, a class which can be used to track the parsing process. In order to use it, create a subclass for the generated listener and implement necessary hook methods:

class Listener(MyGrammarListener):
def __init__(self):
self._initial_state = MyState()

def enterMyExpression(self, ctx):

def exitMyExpression(self, ctx):

Check out `` to see a real example, it's rather straightforward.

## Development

HYAML works with Python 3.7+. Install Python and pip then run

pip install -e .[dev]

## Packaging and publishing

According to the [docs](, run the following commands:

pip install --upgrade setuptools wheel twine
python3 sdist bdist_wheel

You may want to publish the package to the Test PyPi repo first:

twine upload --repository-url dist/*

If you sure and ready, publish it to the main index:

twine upload --repository-url dist/*

Note that for publishing you'll be asked for your credentials for access to PyPi repositories. See additional [instructions]( on managing credentials.

## Contributing

Bug reports and pull requests are welcome on GitHub at <>.

Project details

Release history Release notifications

Download files

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

Files for hyaml, version 0.1.5
Filename, size File type Python version Upload date Hashes
Filename, size hyaml-0.1.5-py3-none-any.whl (26.6 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size hyaml-0.1.5.tar.gz (23.9 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page