Skip to content

Commit

Permalink
chore: reorganize folder structure
Browse files Browse the repository at this point in the history
  • Loading branch information
eWert-Online committed Mar 31, 2024
1 parent 9d3e9bc commit c4b7b34
Show file tree
Hide file tree
Showing 43 changed files with 202 additions and 223 deletions.
File renamed without changes.
File renamed without changes.
6 changes: 6 additions & 0 deletions lib/00_source/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(library
(name Pinc_Source)
(public_name pinc-lang.source)
(libraries)
(instrumentation
(backend landmarks --auto)))
65 changes: 65 additions & 0 deletions lib/01_diagnostics/Location.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
module Position = struct
type t = {
source : Pinc_Source.t;
line : int;
column : int;
}

let make ~source ~line ~column = { source; line; column }
let get_line pos = pos.line
let get_column pos = pos.column
let get_source pos = pos.source
end

type t = {
loc_start : Position.t;
loc_end : Position.t;
}

let merge ~s ~e () = { loc_start = s.loc_start; loc_end = e.loc_end }

let make ?e ~s () =
match e with
| None -> { loc_start = s; loc_end = s }
| Some e -> { loc_start = s; loc_end = e }
;;

let none =
{
loc_start = Position.make ~source:Pinc_Source.empty ~line:0 ~column:0;
loc_end = Position.make ~source:Pinc_Source.empty ~line:0 ~column:0;
}
;;

let get_source loc = loc.loc_start.source
let get_start loc = loc.loc_start
let get_end loc = loc.loc_end

let pp ppf loc =
match (Pinc_Source.name loc.loc_start.source, loc = none) with
| None, _ | _, true -> ()
| Some filename, _ ->
let loc_string =
if loc.loc_start.line = loc.loc_end.line then
if loc.loc_start.column = loc.loc_end.column then
Format.sprintf "%i:%i" loc.loc_start.line loc.loc_start.column
else
Format.sprintf
"%i:%i-%i"
loc.loc_start.line
loc.loc_start.column
loc.loc_end.column
else
Format.sprintf
"%i:%i-%i:%i"
loc.loc_start.line
loc.loc_start.column
loc.loc_end.line
loc.loc_end.column
in
Fmt.pf
ppf
"%a"
Fmt.(styled `Faint string)
(Printf.sprintf "in file %s:%s" filename loc_string)
;;
18 changes: 18 additions & 0 deletions lib/01_diagnostics/Location.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module Position : sig
type t

val make : source:Pinc_Source.t -> line:int -> column:int -> t
val get_line : t -> int
val get_column : t -> int
val get_source : t -> Pinc_Source.t
end

type t

val merge : s:t -> e:t -> unit -> t
val make : ?e:Position.t -> s:Position.t -> unit -> t
val none : t
val get_source : t -> Pinc_Source.t
val get_start : t -> Position.t
val get_end : t -> Position.t
val pp : Format.formatter -> t -> unit
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
module Source = Pinc_Core.Source
module Location = Pinc_Location
module Position = Pinc_Position
module Location = Location
module Source = Pinc_Source

let print_code ~color ~loc source_code =
let context_lines = 1 in
let start_pos = loc.Location.loc_start in
let end_pos = loc.Location.loc_end in
let highlight_line_start = start_pos.line in
let highlight_line_end = end_pos.line in
let highlight_column_start = start_pos.column in
let highlight_column_end = end_pos.column in
let start_pos = loc |> Location.get_start in
let end_pos = loc |> Location.get_end in
let highlight_line_start = start_pos |> Location.Position.get_line in
let highlight_line_end = end_pos |> Location.Position.get_line in
let highlight_column_start = start_pos |> Location.Position.get_column in
let highlight_column_end = end_pos |> Location.Position.get_column in

let first_shown_line = start_pos.line - context_lines |> Int.max 0 in
let last_shown_line = end_pos.line + context_lines in
let first_shown_line = highlight_line_start - context_lines |> Int.max 0 in
let last_shown_line = highlight_line_end + context_lines in

let lines =
source_code
Expand Down Expand Up @@ -97,35 +96,6 @@ let print_code ~color ~loc source_code =
Buffer.contents buf
;;

let print_loc ppf (loc : Location.t) =
match (Source.name loc.loc_start.source, loc = Location.none) with
| None, _ | _, true -> ()
| Some filename, _ ->
let loc_string =
if loc.loc_start.line = loc.loc_end.line then
if loc.loc_start.column = loc.loc_end.column then
Format.sprintf "%i:%i" loc.loc_start.line loc.loc_start.column
else
Format.sprintf
"%i:%i-%i"
loc.loc_start.line
loc.loc_start.column
loc.loc_end.column
else
Format.sprintf
"%i:%i-%i:%i"
loc.loc_start.line
loc.loc_start.column
loc.loc_end.line
loc.loc_end.column
in
Fmt.pf
ppf
"%a"
Fmt.(styled `Faint string)
(Printf.sprintf "in file %s:%s" filename loc_string)
;;

let print_header ppf ~color text =
Fmt.pf ppf "%a" Fmt.(styled `Bold (styled color string)) text
;;
Expand All @@ -144,9 +114,28 @@ let print ~kind ppf (loc : Location.t) =
| `error -> "ERROR"
in
Fmt.pf ppf "@[%a@] " (print_header ~color) header;
Fmt.pf ppf "@[%a@]@," print_loc loc;
Fmt.pf ppf "@[%a@]@," Location.pp loc;

let source_code = Source.content loc.loc_start.source in
let source_code = loc |> Location.get_source |> Source.content in
if source_code <> "" then
Fmt.pf ppf "@,%s" (print_code ~color ~loc source_code)
;;

let set_renderer ppf =
match Sys.getenv_opt "NO_COLOR" with
| None | Some "" -> Fmt.set_style_renderer ppf `Ansi_tty
| Some _ -> Fmt.set_style_renderer ppf `None
;;

let error location message =
let ppf = Format.err_formatter in
set_renderer ppf;
Fmt.pf ppf "@[<v>@,%a@,%s@,@]" (print ~kind:`error) location message;
exit 1
;;

let warn location message =
let ppf = Format.err_formatter in
set_renderer ppf;
Fmt.pf ppf "@[<v>@,%a@,%s@,@]" (print ~kind:`warning) location message
;;
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Location = Pinc_Location
module Location = Location

val error : Location.t -> string -> 'a
val warn : Location.t -> string -> unit
2 changes: 1 addition & 1 deletion lib/pinc_diagnostics/dune → lib/01_diagnostics/dune
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(library
(name Pinc_Diagnostics)
(public_name pinc-lang.diagnostics)
(libraries Pinc_Core fmt)
(libraries Pinc_Source fmt)
(instrumentation
(backend bisect_ppx)))
19 changes: 9 additions & 10 deletions lib/pinc_frontend/Pinc_Ast.ml → lib/02_parsing/Ast.ml
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
module Location = Pinc_Diagnostics.Location
module Operators = Pinc_Operators
module Operators = Operators

type uppercase_identifier = Uppercase_Id of (string * Location.t)
and lowercase_identifier = Lowercase_Id of (string * Location.t)
type uppercase_identifier = Uppercase_Id of (string * Pinc_Diagnostics.Location.t)
and lowercase_identifier = Lowercase_Id of (string * Pinc_Diagnostics.Location.t)

and template_node = {
template_node_loc : Location.t;
template_node_loc : Pinc_Diagnostics.Location.t;
template_node_desc : template_node_desc;
}

Expand All @@ -25,7 +24,7 @@ and template_node_desc =
| TextTemplateNode of string

and tag = {
tag_loc : Location.t;
tag_loc : Pinc_Diagnostics.Location.t;
tag_desc : tag_desc;
}

Expand All @@ -51,7 +50,7 @@ and tag_desc = {
}

and string_template = {
string_template_loc : Location.t;
string_template_loc : Pinc_Diagnostics.Location.t;
string_template_desc : string_template_desc;
}

Expand All @@ -60,7 +59,7 @@ and string_template_desc =
| StringText of string

and expression = {
expression_loc : Location.t;
expression_loc : Pinc_Diagnostics.Location.t;
expression_desc : expression_desc;
}

Expand Down Expand Up @@ -103,7 +102,7 @@ and expression_desc =
| BinaryExpression of expression * Operators.Binary.t * expression

and statement = {
statement_loc : Location.t;
statement_loc : Pinc_Diagnostics.Location.t;
statement_desc : statement_desc;
}

Expand All @@ -119,7 +118,7 @@ and statement_desc =
| ExpressionStatement of expression

and declaration = {
declaration_loc : Location.t;
declaration_loc : Pinc_Diagnostics.Location.t;
declaration_type : declaration_type;
}

Expand Down
3 changes: 1 addition & 2 deletions lib/pinc_frontend/Pinc_Lexer.ml → lib/02_parsing/Lexer.ml
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module Diagnostics = Pinc_Diagnostics
module Location = Diagnostics.Location
module Token = Pinc_Token
module Source = Pinc_Core.Source
module Source = Pinc_Source

type mode =
| Normal
Expand Down
4 changes: 4 additions & 0 deletions lib/02_parsing/Lexer.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type t

val make : Pinc_Source.t -> t
val scan : t -> Token.t
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
module Token = Pinc_Token

type precedence = int

type associativity =
Expand Down
Loading

0 comments on commit c4b7b34

Please sign in to comment.