Skip to content

jevko/jevko.lua

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

14 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

jevko.lua logo

jevko.lua

A single-file Jevko library for Lua. Inspired by rxi/json.lua.

Encodes and decodes plain Jevko.

Features

  • Simple
  • Lightweight: ~3 KiB, < 100 SLOC
  • Error messages with line:column information, e.g. Invalid digraph (`x) at 1:10!

Usage

Copy the jevko.lua file into your project and require it:

jevko = require "jevko"

You can now use the following functions:

jevko.from_string(str)

Aliases: jevko.decode, jevko.fromString.

Turns a string into a Jevko parse tree, e.g.:

jevko.from_string([=[
id [10]
colors [
  [red]
  [yellow]
  [blue]
]
]=])

returns

{
  subjevkos = {
    {prefix = "id ", jevko = {subjevkos = {}, suffix = "10"}},
    {prefix = "\ncolors ", jevko = {
      subjevkos = {
        {prefix = "\n  ", jevko = {subjevkos = {}, suffix = "red"}},
        {prefix = "\n  ", jevko = {subjevkos = {}, suffix = "yellow"}},
        {prefix = "\n  ", jevko = {subjevkos = {}, suffix = "blue"}},
      },
      suffix = "\n"
    }},
  },
  suffix = "\n"
}

jevko.to_string(jev)

Aliases: jevko.encode, jevko.toString.

Inverse of jevko.from_string.

Serializes a Jevko parse tree into a string, e.g.:

jevko.to_string({
  subjevkos = {
    {prefix = "id ", jevko = {subjevkos = {}, suffix = "10"}},
    {prefix = "\ncolors ", jevko = {
      subjevkos = {
        {prefix = "\n  ", jevko = {subjevkos = {}, suffix = "red"}},
        {prefix = "\n  ", jevko = {subjevkos = {}, suffix = "yellow"}},
        {prefix = "\n  ", jevko = {subjevkos = {}, suffix = "blue"}},
      },
      suffix = "\n"
    }},
  },
  suffix = "\n"
})

returns the string

id [10]
colors [
  [red]
  [yellow]
  [blue]
]

jevko.escape(str)

A helper function which can be used with custom Jevko encoders.

Turns a string into a Jevko-safe string by escaping special characters, e.g.:

jevko.escape("Only these three are special: ` [ ]")

returns the string

Only these three are special: `` `[ `]

See also

easyjevko.lua -- a library for Easy Jevko, a simple format built on Jevko.

License

MIT