DrSensor / universal-formatter

Code (or Text) formatter inspired from grammar in syntax highlighter

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Universal Formatter

An experimental code/text formatter that utilize Rosie Pattern Language

still just an idea

Usage

Given this json5.rpl and json5.yml

package json5

import word, num

local true = "true"
local false = "false"
local null = "null"

string = word.q
number = num.signed_number

key = word.any / string
primitive_value = string / number / true / false / null
member = key ":" primitive_value ","?
member_non_null = key ":" (string / number / true / false) ","?
version: '1'
grammar: [json5.rpl]
rules:
  no_comma:
    scope: json5.key ":" json5.primitive ","
    remove: ","
  null_first:
    scope: "{" json5.memeber "}"
    match_and_place:
      at_start: "{"
      at_end: "}"
    patterns:
    - place: json5.key ":" json5.null ","?
      before: json5.member_non_null
    - match_and_place:
        before_end: json5.member_non_null

then execute

$ echo '{"a":123,"b":null}' | ufmt --grammar json5.yml --enable null_first,no_comma
{
  "b": null
  "a": 123
}

Inspiration

About

Code (or Text) formatter inspired from grammar in syntax highlighter

License:Apache License 2.0