Browse Source

initial commit

main
zseri 2 years ago
commit
0dcf8ef289
15 changed files with 371 additions and 0 deletions
  1. +5
    -0
      .gitignore
  2. +15
    -0
      LICENSE
  3. +57
    -0
      ast.ml
  4. +10
    -0
      default.nix
  5. +4
    -0
      docs/ex.erm
  6. +8
    -0
      dune
  7. +3
    -0
      dune-project
  8. +97
    -0
      ermit.ml
  9. +0
    -0
      ermit.opam
  10. +60
    -0
      flake.lock
  11. +20
    -0
      flake.nix
  12. +31
    -0
      index.nix
  13. +31
    -0
      lex.ml
  14. +20
    -0
      parser.mly
  15. +10
    -0
      shell.nix

+ 5
- 0
.gitignore View File

@ -0,0 +1,5 @@
/_build/
/result
/result-*
/.merlin
/ermit.install

+ 15
- 0
LICENSE View File

@ -0,0 +1,15 @@
ISC License
Copyright (c) 2021, zseri <zseri.devel@ytrizja.de>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

+ 57
- 0
ast.ml View File

@ -0,0 +1,57 @@
let sprintf = Printf.sprintf
type cardinality =
| Optional
| One
| N
type relcol = { rctabnam: string; rccardty: cardinality }
type table =
{ tprimkeys: string list
; tattrs: string list
}
type relation =
{ rname: string
; relates: relcol list
; rattrs: string list
}
type ent =
| Table of string * table
| Relation of relation
module Tables = Map.Make(String)
type ast =
{ atables: table Tables.t
; arelations: relation list
}
let map_and_concat f xs = List.map f xs |> String.concat ""
(* access the last character, raise exception if given string is empty *)
let parse_rtabnam x =
let lcidx = (String.length x) - 1 in
match String.get x lcidx with
| '+' -> { rctabnam = (String.sub x 0 lcidx); rccardty = N }
| '?' -> { rctabnam = (String.sub x 0 lcidx); rccardty = Optional }
| _ -> { rctabnam = x; rccardty = One }
let empty =
{ atables = Tables.empty
; arelations = []
}
let update_ast e a =
match e with
| Table (name, t) -> { a with atables = Tables.add name t a.atables }
| Relation (r) -> { a with arelations = r::a.arelations }
let build_ast_of_list x =
let rec intern = function
| [] -> empty
| x::xs -> (intern xs |> update_ast x)
in
List.rev x |> intern

+ 10
- 0
default.nix View File

@ -0,0 +1,10 @@
(import
(
let
lock = builtins.fromJSON (builtins.readFile ./flake.lock);
in fetchTarball {
url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz";
sha256 = lock.nodes.flake-compat.locked.narHash;
}
)
{ src = ./.; }).defaultNix

+ 4
- 0
docs/ex.erm View File

@ -0,0 +1,4 @@
table GESELLSCHAFT bezeichnug : land
table MASCHINE fznr : wartung
relat besitzt GESELLSCHAFT MASCHINE+ : seit
relat seit : seit

+ 8
- 0
dune View File

@ -0,0 +1,8 @@
(menhir (flags --explain parser.mly) (modules parser))
(executables
(public_names ermit)
(names ermit)
(libraries cmdliner sedlex menhirLib)
(preprocess (pps sedlex.ppx))
(modules ast lex parser ermit))

+ 3
- 0
dune-project View File

@ -0,0 +1,3 @@
(lang dune 2.7)
(using menhir 2.1)
(name ermit)

+ 97
- 0
ermit.ml View File

@ -0,0 +1,97 @@
open Printf
let provider lexbuf () =
let tok = Lex.token lexbuf in
let start, stop = Sedlexing.lexing_positions lexbuf in
tok, start, stop
let parser_result lexbuf = MenhirLib.Convert.Simplified.traditional2revised
Parser.prog
(provider lexbuf)
let print_position outx lexbuf =
let (_, pos) = Sedlexing.lexing_positions lexbuf in
fprintf outx "%s:%d:%d" pos.pos_fname
pos.pos_lnum (pos.pos_cnum - pos.pos_bol + 1)
let parse_with_error lexbuf =
try (parser_result lexbuf; ) with
| Lex.SyntaxError msg ->
eprintf "%a: %s\n" print_position lexbuf msg;
[]
| Parser.Error ->
eprintf "%a: syntax error\n" print_position lexbuf;
[]
exception Rendering_failed of string
let render axs =
let render_node shape name label = sprintf " \"%s\" [shape=%s,label=%s];\n" name shape label in
let render_atgen pfx name label =
let rndrd_coll = sprintf "%s_%s" pfx name in
sprintf "%s \"%s\" -- \"%s\";\n" (render_node "ellipse" rndrd_coll label) pfx rndrd_coll
in
let render_primary pfx name = render_atgen pfx name (sprintf "<<u>%s</u>>" name) in
let render_attr pfx name = render_atgen pfx name (sprintf "\"%s\"" name) in
let render_tab name t = (
let pfx = sprintf "T%s" name in
(render_node "box" pfx name)
::
(List.append
(List.map (render_primary pfx) t.Ast.tprimkeys)
(List.map (render_attr pfx) t.Ast.tattrs))
) in
let render_rel i r = (
let pfx = sprintf "R%d" i in
let qnos = ref 0 in
let render_relcol rc: string = (
let lbl = match rc.Ast.rccardty with
| Optional | One -> "1"
| N -> (
let oqnos = !qnos in
let x = match oqnos with
| 0 -> "n"
| 1 -> "m"
| 2 -> "o"
| 3 -> "p"
| _ -> raise (Rendering_failed (sprintf "attempted to render %d. n-cardinality" oqnos))
in let () = (qnos := (oqnos + 1)) in
x
)
in
sprintf " \"%s\" -- \"T%s\" [label=\"%s\",len=3.0];\n" pfx rc.Ast.rctabnam lbl
) in
(render_node "diamond" pfx r.Ast.rname)
::
(List.append
(List.map render_relcol r.Ast.relates)
(List.map (render_attr pfx) r.Ast.rattrs))
) in
(sprintf "graph ER {
layout=neato
%s}" (String.concat "" ((List.append
(Ast.Tables.to_seq axs.Ast.atables |> Seq.map (fun (name, t) -> render_tab name t) |> List.of_seq)
(List.mapi render_rel axs.Ast.arelations)
) |> List.flatten |> List.map (sprintf " %s;\n"))))
let perfile filename () =
let inx = Stdlib.open_in filename in
let lexbuf = Sedlexing.Utf8.from_channel inx in
Sedlexing.set_filename lexbuf filename;
parse_with_error lexbuf |> Ast.build_ast_of_list |> render |> print_endline;
Stdlib.close_in inx
module Arg = Cmdliner.Arg
module Term = Cmdliner.Term
let files = Arg.(non_empty & pos_all file [] & info [] ~docv:"FILE")
let rec main files = match files with
| [] -> ()
| x::xs -> perfile x (); main xs
let () =
let doc = "parse and render" in
(Term.(const main $ files),
Term.info "ermit" ~version:"v0.0.0" ~doc ~exits:Term.default_exits)
|> Term.eval |> Term.exit

+ 0
- 0
ermit.opam View File


+ 60
- 0
flake.lock View File

@ -0,0 +1,60 @@
{
"nodes": {
"flake-compat": {
"flake": false,
"locked": {
"lastModified": 1606424373,
"narHash": "sha256-oq8d4//CJOrVj+EcOaSXvMebvuTkmBJuT5tzlfewUnQ=",
"owner": "edolstra",
"repo": "flake-compat",
"rev": "99f1c2157fba4bfe6211a321fd0ee43199025dbf",
"type": "github"
},
"original": {
"owner": "edolstra",
"repo": "flake-compat",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1618975354,
"narHash": "sha256-U0WdVfyBCgjqj/ukkEiaLjR8aHxrkWAN1BHETRUuPmA=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "04bb7dc19bc7b382f76b186e9674397a4f371f4a",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixpkgs-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"flake-compat": "flake-compat",
"nixpkgs": "nixpkgs",
"yz-flake-utils": "yz-flake-utils"
}
},
"yz-flake-utils": {
"locked": {
"lastModified": 1619013372,
"narHash": "sha256-fl5eNW1R91WXRRdoWwwbe7BNytO92RlYRXE730vxoeo=",
"owner": "YZITE",
"repo": "flake-utils",
"rev": "5fc309194a7c9698587ffd22940c70aa10092a46",
"type": "github"
},
"original": {
"owner": "YZITE",
"repo": "flake-utils",
"type": "github"
}
}
},
"root": "root",
"version": 7
}

+ 20
- 0
flake.nix View File

@ -0,0 +1,20 @@
{
description = "ocaml stuff";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
yz-flake-utils.url = "github:YZITE/flake-utils";
# needed for default.nix, shell.nix
flake-compat = {
url = "github:edolstra/flake-compat";
flake = false;
};
};
outputs = { nixpkgs, yz-flake-utils, ... }:
(yz-flake-utils.lib.mkFlakeFromProg {
prevpkgs = nixpkgs;
progname = "ermit";
drvBuilder = final: prev: (final.pkgs.callPackage ./index.nix {
#ocamlPackages = final.ocaml-ng.ocamlPackages_4_11;
});
});
}

+ 31
- 0
index.nix View File

@ -0,0 +1,31 @@
{
lib,
stdenv,
ocamlPackages,
}:
ocamlPackages.buildDunePackage rec {
pname = "ermit";
version = "unstable";
useDune2 = true;
src = ./.;
minimumOCamlVersion = "4.09";
nativeBuildInputs = with ocamlPackages; [
menhir
];
buildInputs = with ocamlPackages; [
cmdliner menhir sedlex_2
];
meta = with lib; {
description = "ERM emitter";
homepage = "https://git.ytrizja.de/zseri/ermit";
license = licenses.mit;
maintainers = maintainers.zseri;
platforms = platforms.unix;
};
}

+ 31
- 0
lex.ml View File

@ -0,0 +1,31 @@
let digit = [%sedlex.regexp? '0'..'9']
let number = [%sedlex.regexp? Plus digit]
let float = [%sedlex.regexp? number, '.', Opt( number ), Opt ( 'e', number )]
let lexeme = Sedlexing.Utf8.lexeme
let ident = [%sedlex.regexp? id_start, Star ( id_continue ), Opt ( '+' | '?' )]
exception SyntaxError of string
let rec token_inner buf =
match%sedlex buf with
| eof -> Parser.EOF
| '\n' -> Parser.NL
| "table" -> Parser.TABLE
| "relat" -> Parser.RELAT
| ":" -> Parser.DCOLON
| white_space -> token_inner buf
| ident -> Parser.ID (lexeme buf)
| _ -> raise (SyntaxError ("Unexpected char: " ^ lexeme buf))
let token buf =
let r = token_inner buf in
let tst = match r with
| Parser.EOF -> "."
| Parser.NL -> "\\n"
| Parser.TABLE -> "table"
| Parser.RELAT -> "relat"
| Parser.DCOLON -> ":"
| Parser.ID x -> Printf.sprintf "id(%s)" x
in
Printf.printf "got token %s\n" tst; r

+ 20
- 0
parser.mly View File

@ -0,0 +1,20 @@
%token <string> ID
%token NL
%token TABLE "table"
%token RELAT "relat"
%token DCOLON ":"
%token EOF
%start <Ast.ent list> prog
%%
prog: list(NL); es = separated_list(nls, ent); EOF { es };
nls: nonempty_list(NL) { };
rtablename: x = ID { Ast.parse_rtabnam x };
ent:
| TABLE; t = ID; pks = list(ID); DCOLON; attrs = list(ID) { Ast.Table (t, { tprimkeys = pks; tattrs = attrs }) }
| RELAT; r = ID; rts = list(rtablename); DCOLON; attrs = list(ID) { Ast.Relation { rname = r; relates = rts; rattrs = attrs } }
;

+ 10
- 0
shell.nix View File

@ -0,0 +1,10 @@
(import
(
let
lock = builtins.fromJSON (builtins.readFile ./flake.lock);
in fetchTarball {
url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz";
sha256 = lock.nodes.flake-compat.locked.narHash;
}
)
{ src = ./.; }).shellNix.default