XCHEME Lang is a programming language designed for developing lexers and parsers for other languages, it can be a brand new language or some from the market. This article will help you to get started with XCHEME and furthers your understanding of what can be achieved with this technology.
XCHEME Lang (Or just "XCHEME") is a set of case-sensitive directives and expressions commonly used for validating portions of strings, each directive is an atomic pattern (a small action, e.g: repeat
, expect
, choose
, etc...) that must be valid for making the analysis process to move on up to the end of the string. By using atomic patterns, it's possible to combine a variety of statements to create complex validation rules that will produce a list of tokens and nodes already organized an AST (Abstract Syntax Tree) for post-processing.
- Install the CLI.
npm i @xcheme/cli -g
- Create a new file
parser.xcm
(or download the sample) and save it with the following contents.
skip ' ' | '\r' | '\n';
token <1> T_HELLO as uncase 'hello';
token <2> T_WORLD as uncase 'world';
token <3> T_EXCL_POINT as '!';
token <4> T_PERIOD as '.';
node <1> HELLO_WORLD as T_HELLO & T_WORLD & opt (T_EXCL_POINT | T_PERIOD);
- Create a new file
input.txt
(or download the sample) and save it with the following contents.
hello world!
Hello World.
Hello world
- Test your parser by feeding it the input file.
xcm -s parser.xcm -t input.txt --run --tokens --nodes
- Check the output after the consumption process.
Tokens:
Code Fragment
0:0 0001 "Hello"
0:6 0002 "World"
0:11 0003 "!"
1:0 0001 "Hello"
1:6 0002 "World"
1:11 0004 "."
2:0 0001 "Hello"
2:6 0002 "World"
Nodes:
0:0 N 1 "Hello World!"
1:0 N 1 "Hello World."
2:0 N 1 "Hello World"
Done!
In basic terms, we've just used XCHEME to make a parser that's able to consume a combination of case-insensitive hello world sentences, and also printed all the resulting tokens and nodes of the analysis process... To get a really deep understanding of what happened, why it happened that way, and start creating your own lexer and/or parser, take a time to delve into the next steps.