about summary refs log tree commit diff
path: root/boot/lex.c
diff options
context:
space:
mode:
Diffstat (limited to 'boot/lex.c')
-rw-r--r--boot/lex.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/boot/lex.c b/boot/lex.c
index 9a94c55..e8432f6 100644
--- a/boot/lex.c
+++ b/boot/lex.c
@@ -28,6 +28,30 @@ span_width(Pos start, uint width)
     return (struct Span){ .start = start, .end = start + width };
 }
 
+// create a span which encompasses two spans.
+struct Span
+span_merge(struct Span a, struct Span b)
+{
+    return (struct Span){
+        .start = a.start < b.start ? a.start : b.start,
+        .end = a.end > b.end ? a.end : b.end,
+    };
+}
+
+// expand a span by a number of bytes.
+// negative number expands the span to the left,
+// positive number expands the span to the right.
+struct Span
+span_expand(struct Span span, integer by)
+{
+    uint start_expand = by < 0 ? -by : 0;
+    uint end_expand = by > 0 ? by : 0;
+    return (struct Span){
+        .start = span.start - start_expand,
+        .end = span.end + end_expand,
+    };
+}
+
 // a cursor position placed within a text file.
 struct Cursor
 {
@@ -235,6 +259,30 @@ token_simple(enum Token_Kind kind, Pos pos, struct Cursor location)
     return token_wide(kind, (struct Span){ pos, pos }, location);
 }
 
+struct Token
+token_none()
+{
+    return token_simple(TOKEN_NONE, 0, (struct Cursor){ 0 });
+}
+
+bool
+token_is(const struct Token* t, enum Token_Kind kind)
+{
+    return t->kind == kind;
+}
+
+bool
+token_is_empty(const struct Token* t)
+{
+    return token_is(t, TOKEN_NONE);
+}
+
+bool
+token_ends_statement(const struct Token* t)
+{
+    return token_is(t, TOKEN_END_OF_FILE) || token_is(t, TOKEN_NEWLINE);
+}
+
 bool
 ascii_in_range(ascii c, ascii from, ascii to)
 {