Skip to content

Commit e522453

Browse files
phatedospencer
andauthored
chore(compiler): Refactor literals in the lexer (#2030)
Co-authored-by: Oscar Spencer <oscar@grain-lang.org>
1 parent 11c4ba9 commit e522453

File tree

20 files changed

+899
-582
lines changed

20 files changed

+899
-582
lines changed

compiler/src/formatting/format.re

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2564,7 +2564,7 @@ and get_function_name = (expr: Parsetree.expression) => {
25642564
switch (expr.pexp_desc) {
25652565
| PExpConstant(x) =>
25662566
switch (x) {
2567-
| PConstString(str) => str
2567+
| PConstString({txt: str}) => str
25682568
| _ => ""
25692569
}
25702570

compiler/src/parsing/ast_helper.re

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ let record_pattern_info = record_pats =>
5858
([], Asttypes.Closed),
5959
);
6060

61+
// This normalizes STRING items in the parsetree that aren't constants so we don't need
62+
// to scatter them throughout the rest of the compiler.
63+
let normalize_string = (~loc, item) => {
64+
switch (Grain_utils.Literals.conv_string(item.txt)) {
65+
| Ok(i) => {loc: item.loc, txt: i}
66+
| Error(msg) => raise(SyntaxError(loc, msg))
67+
};
68+
};
69+
6170
module Constant = {
6271
let bytes = b => PConstBytes(b);
6372
let string = s => PConstString(s);
@@ -67,25 +76,18 @@ module Constant = {
6776
let int16 = i => PConstInt16(i);
6877
let int32 = i => PConstInt32(i);
6978
let int64 = i => PConstInt64(i);
70-
let uint8 = (is_neg, i) => PConstUint8(is_neg, i);
71-
let uint16 = (is_neg, i) => PConstUint16(is_neg, i);
72-
let uint32 = (is_neg, i) => PConstUint32(is_neg, i);
73-
let uint64 = (is_neg, i) => PConstUint64(is_neg, i);
79+
let uint8 = i => PConstUint8(i);
80+
let uint16 = i => PConstUint16(i);
81+
let uint32 = i => PConstUint32(i);
82+
let uint64 = i => PConstUint64(i);
7483
let float32 = f => PConstFloat32(f);
7584
let float64 = f => PConstFloat64(f);
7685
let wasmi32 = i => PConstWasmI32(i);
7786
let wasmi64 = i => PConstWasmI64(i);
7887
let wasmf32 = f => PConstWasmF32(f);
7988
let wasmf64 = f => PConstWasmF64(f);
8089
let bigint = i => PConstBigInt(i);
81-
let rational = r => {
82-
let (n, d) =
83-
switch (String.split_on_char('/', r)) {
84-
| [n, d] => (n, d)
85-
| _ => failwith("Impossible: rational literal without forward slash")
86-
};
87-
PConstRational(n, d);
88-
};
90+
let rational = r => PConstRational(r);
8991
let bool = b => PConstBool(b);
9092
let void = PConstVoid;
9193
};
@@ -467,14 +469,18 @@ module Toplevel = {
467469

468470
module PrimitiveDescription = {
469471
let mk = (~loc, ~ident, ~name, ()) => {
470-
{pprim_ident: ident, pprim_name: name, pprim_loc: loc};
472+
{
473+
pprim_ident: ident,
474+
pprim_name: normalize_string(~loc, name),
475+
pprim_loc: loc,
476+
};
471477
};
472478
};
473479

474480
module ValueDescription = {
475481
let mk = (~loc, ~mod_, ~name, ~alias, ~typ, ()) => {
476482
{
477-
pval_mod: mod_,
483+
pval_mod: normalize_string(~loc, mod_),
478484
pval_name: name,
479485
pval_name_alias: alias,
480486
pval_type: typ,
@@ -497,7 +503,11 @@ module MatchBranch = {
497503

498504
module IncludeDeclaration = {
499505
let mk = (~loc, path, alias) => {
500-
{pinc_alias: alias, pinc_path: path, pinc_loc: loc};
506+
{
507+
pinc_alias: alias,
508+
pinc_path: normalize_string(~loc, path),
509+
pinc_loc: loc,
510+
};
501511
};
502512
};
503513

@@ -545,3 +555,9 @@ module ModuleDeclaration = {
545555
{pmod_name: name, pmod_stmts: stmts, pmod_loc: loc};
546556
};
547557
};
558+
559+
module Attribute = {
560+
let mk = (~loc, name, args) => {
561+
(name, List.map(normalize_string(~loc), args));
562+
};
563+
};

compiler/src/parsing/ast_helper.rei

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -35,26 +35,26 @@ type str = loc(string);
3535
type loc = Location.t;
3636

3737
module Constant: {
38-
let bytes: string => constant;
39-
let string: string => constant;
40-
let char: string => constant;
38+
let bytes: str => constant;
39+
let string: str => constant;
40+
let char: str => constant;
4141
let number: number_type => constant;
42-
let int8: string => constant;
43-
let int16: string => constant;
44-
let int32: string => constant;
45-
let int64: string => constant;
46-
let uint8: (bool, string) => constant;
47-
let uint16: (bool, string) => constant;
48-
let uint32: (bool, string) => constant;
49-
let uint64: (bool, string) => constant;
50-
let float32: string => constant;
51-
let float64: string => constant;
52-
let wasmi32: string => constant;
53-
let wasmi64: string => constant;
54-
let wasmf32: string => constant;
55-
let wasmf64: string => constant;
56-
let bigint: string => constant;
57-
let rational: string => constant;
42+
let int8: str => constant;
43+
let int16: str => constant;
44+
let int32: str => constant;
45+
let int64: str => constant;
46+
let uint8: str => constant;
47+
let uint16: str => constant;
48+
let uint32: str => constant;
49+
let uint64: str => constant;
50+
let float32: str => constant;
51+
let float64: str => constant;
52+
let wasmi32: str => constant;
53+
let wasmi64: str => constant;
54+
let wasmf32: str => constant;
55+
let wasmf64: str => constant;
56+
let bigint: str => constant;
57+
let rational: str => constant;
5858
let bool: bool => constant;
5959
let void: constant;
6060
};
@@ -380,3 +380,7 @@ module LambdaArgument: {
380380
module ModuleDeclaration: {
381381
let mk: (~loc: loc, str, list(toplevel_stmt)) => module_declaration;
382382
};
383+
384+
module Attribute: {
385+
let mk: (~loc: loc, str, list(str)) => attribute;
386+
};

0 commit comments

Comments
 (0)