PDML Examples

Author: Christian Neumanns

Published: 2021-11-16

Introduction

This document shows simple examples of PDML code.

All examples only use Core PDML. PDML extensions are not used.

Fundamental Examples

Hello

[hello]

The above code illustrates the simplest possible PDML document.

It's a root node with name hello and no content (no child nodes). hello is an empty root node.

Text node

[teachers Bob]

The node with name teachers contains the text Bob. The node's name and text are separated by a space character.

A node's text can contain spaces:

[teachers Bob and Alice]

Now node teachers contains the text Bob and Alice

New lines and Unicode characters can also be used in a node's text:

[info He said:
    "She said: 'All is well!'" 😃]

In the above example node info contains the text:

He said:
    "She said: 'All is well!'" 😃

Node with one child node

[config [color orange]]

Node config has one child node with name color and text orange.

Tree

[config[color orange][size[width 1618][height 1000]]]

Node config has two child nodes: color and size. Node size has two child nodes too: width and height.

Readability and writeabilty are considerably improved when indents are used to visualize the structure:

[config
    [color orange]
    [size
        [width 1618]
        [height 1000]
    ]
]

Now the AST has additional text nodes containing whitespace, but whitespace nodes like this are typically ignored by applications reading config data.

Markup code

Suppose we want to render the following text in a browser:

We can write text in italic, bold, or italic and bold.

In HTML we would write:

<div>We can write text in <i>italic</i>, <b>bold</b>, or <i><b>italic and bold</b></i>.</div>

In PDML the code becomes:

[div We can write text in [i italic], [b bold], or [i [b italic and bold]].]

Escape characters in text

If node p contains the text:

    foo\bar[12]

... then [, ], and \ must be escaped:

[p foo\\bar\[12\]]

Real World Examples

Config File

Consider the following JSON config file used to define a database connection:

File config.json
{
    "DB_config": {
        "provider": "MySQL",
        "host": "localhost",
        "port": "3306",
        "database_name": "bookstore",
        "credentials": {
            "user": "Bob",
            "password": null
        },
        "cache": false
    }
}

In PDML the file looks like this:

File config.pdml
[DB_config
    [provider MySQL]
    [host localhost]
    [port 3306]
    [database_name bookstore]
    [credentials
        [user Bob]
        [password]
    ]
    [cache false]
]

Table Data

Say we have the following XML document to store a list of books:

<?xml version="1.0" encoding="UTF-8"?>
<books>
    <book>
        <isbn>978-0135957059</isbn>
        <title>The Pragmatic Programmer: Your Journey to Mastery</title>
        <price>41.41</price>
    </book>
    <book>
        <isbn>978-0735619678</isbn>
        <title>Code Complete: A Practical Handbook of Software Construction</title>
        <price>45.32</price>
    </book>
    <book>
        <isbn>978-0134685991</isbn>
        <title>Effective Java</title>
        <price>44.10</price>
    </book>
</books>

Here are the same data stored in PDML:

[books
    [book
        [isbn 978-0135957059]
        [title The Pragmatic Programmer: Your Journey to Mastery]
        [price 41.41]
    ]
    [book
        [isbn 978-0735619678]
        [title Code Complete: A Practical Handbook of Software Construction]
        [price 45.32]
    ]
    [book
        [isbn 978-0134685991]
        [title Effective Java]
        [price 44.10]
    ]
]

Database

Sometimes it makes sense to use text files (instead of a powerful database engine) to store simple, small databases. Each table could be stored in a separate file, or a single file could be used to store all tables. Text files are also useful to export data.

The following code illustrates a small database stored in PDML format:

[mini_ERP_data
    [customers
        [customer ...]
        [customer ...]
        [customer ...]
    ]
    [suppliers
        [supplier ...]
        [supplier ...]
    ]
    [products
        [product ...]
        [product ...]
        [product ...]
        [product ...]
    ]
]

Markup Code

PML is a markup language that uses PDML's core syntax and extensions.

This document is written in PML. You can have a look at its PML markup code.

Here is an excerpt showing the markup code for this chapter:

[ch [title Markup Code]

    [link (url=https://www.pml-lang.dev/) PML] is a markup language that uses PDML's core syntax and extensions.

    This document is written in PML. You can have a look at its [link (url=https://github.com/pdml-lang/pdml-lang.github.io/tree/main/work/docs/core/Examples/input) PML markup code].

    Here is an excerpt showing the markup code for this chapter:

    [code
        ...
    ]
]

AST

Every PDML document is a tree of data. Hence all kinds of structured and unstructured data can be stored.

An example would be an abstract syntax tree (AST). Suppose we want to represent the arithmetic expression

(1 + 2) * (3 + 4 + 5)

This can be done with the following PDML code:

[exp
    [op *
        [op +
            [num 1]
            [num 2]
        ]
        [op +
            [num 3]
            [num 4]
            [num 5]
        ]
    ]
]

If the code doesn't need to be read by humans, it can be written more succinctly, without indents:

[exp[op *[op +[num 1][num 2]][op +[num 3][num 4][num 5]]]]