Skip to content

Commit

Permalink
parser tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
priyasrikumar committed Aug 3, 2023
1 parent 981d0c6 commit 63676de
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 10 deletions.
10 changes: 7 additions & 3 deletions vcalyx/ocaml/lexer.mll
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
{
open Parser
exception ParseError of string
exception SyntaxError of string
}

let id = ['a'-'z' 'A'-'Z' '_'] ['a'-'z' 'A'-'Z' '0'-'9' '_' '.']*
let whitespace = [' ' '\t']+
let newline = '\r' | '\n' | "\r\n"

rule tokens = parse
(* i.e., 1'd1 *)
| ['0'-'9']+"'d"['0'-'9']+ as i { INT (int_of_string i) }
| ['0'-'9']+ as i { INT (int_of_string i) }
| whitespace { tokens lexbuf }
| newline { Lexing.new_line lexbuf; tokens lexbuf }
| "(" { LPAREN }
| ")" { RPAREN }
| "components" { COMPONENTS }
Expand Down Expand Up @@ -41,4 +45,4 @@ rule tokens = parse
| "assignments" { ASSIGNMENTS }
| eof { EOF }
| id as x { ID x }
| _ { raise (ParseError (Printf.sprintf "At offset %d: unexpected character.\n" (Lexing.lexeme_start lexbuf))) }
| _ { raise (SyntaxError (Printf.sprintf "At offset %d: unexpected character %s" (Lexing.lexeme_start lexbuf) (Lexing.lexeme lexbuf))) }
4 changes: 2 additions & 2 deletions vcalyx/ocaml/parser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
%token DIRECTION
%token ASSIGNMENTS

%start <Extr.context> main
%start <Extr.context option> main
%%

main:
| LPAREN; LPAREN; COMPONENTS; LPAREN; comps = list(component); RPAREN; RPAREN;
LPAREN; ENTRYPOINT; entry = ID; RPAREN; RPAREN; EOF
{ {ctx_comps = comps; ctx_entrypoint = entry} }
{ Some {ctx_comps = comps; ctx_entrypoint = entry} }

attrs_clause:
| LPAREN; ATTRIBUTES; LPAREN; attrs = list(attribute); RPAREN; RPAREN
Expand Down
32 changes: 27 additions & 5 deletions vcalyx/ocaml/vcx.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,28 @@
open Core
open Vcalyx
open Lexing

(* from https://dev.realworldocaml.org/parsing-with-ocamllex-and-menhir.html *)
let print_position outx lexbuf =
let pos = lexbuf.lex_curr_p 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.main Lexer.tokens lexbuf with
(* | SyntaxError msg ->
fprintf stderr "%a: %s\n" print_position lexbuf msg;
None *)
| Parser.Error ->
fprintf stderr "%a: syntax error\n" print_position lexbuf;
exit (-1)

let rec parse_and_print source_str source_location =
match parse_with_error source_str with
| Some _ ->
Printf.printf "Successfully parsed %s.\n" source_location;
parse_and_print source_str source_location
| None -> ()

let vcx_parse : Command.t =
let open Command.Let_syntax in
Expand All @@ -9,14 +32,13 @@ let vcx_parse : Command.t =
fun () ->
let source_chan = In_channel.create source_location in
let source_str = Lexing.from_channel source_chan in
source_str.lex_curr_p <- { source_str.lex_curr_p with pos_fname = source_location };
In_channel.close source_chan;
match Parser.main Lexer.tokens source_str with
| _ -> Printf.printf "Successfully parsed %s.\n" source_location]
source_str.lex_curr_p <-
{ source_str.lex_curr_p with pos_fname = source_location };
parse_and_print source_str source_location;
In_channel.close source_chan]

let vcx_cmd : Command.t =
Command.group ~summary:"vcx: the vcalyx command-line interface"
[ ("parse", vcx_parse) ]
(* ("json-test", json_test) *)

let () = Command_unix.run ~version:"dev" vcx_cmd

0 comments on commit 63676de

Please sign in to comment.