commit 1dfe3c590a36aa11a01e912656ce0069374384a9
parent 9f012042269d72e38f9fd0c6e5962c54af2933a0
Author: citbl <citbl@citbl.org>
Date: Fri, 5 Jun 2026 17:22:38 +1000
parser wip
Diffstat:
1 file changed, 38 insertions(+), 8 deletions(-)
diff --git a/src/parser.adb b/src/parser.adb
@@ -3,10 +3,16 @@ with Lexer; use Lexer;
package body Parser is
function Init
- (File_Name : File_Name_Ref; Source : Source_Ref; Tokens : Token_Vector.Vector) return Parser
+ (File_Name : File_Name_Ref;
+ Source : Source_Ref;
+ Tokens : Token_Vector.Vector) return Parser
is
P : Parser :=
- (Source => Source, File_Name => File_Name, Tokens => Tokens, Pos => 1, AST => null);
+ (Source => Source,
+ File_Name => File_Name,
+ Tokens => Tokens,
+ Pos => 1,
+ AST => null);
begin
return P;
end Init;
@@ -17,9 +23,13 @@ package body Parser is
begin
if P.Pos < P.Tokens.Last_Index then
return P.Tokens (P.Pos);
- else
- return Token'(Kind => EOF, Lexeme => To_Unbounded_String (""), Line => 1, Col => 1);
end if;
+ return
+ Token'
+ (Kind => EOF,
+ Lexeme => To_Unbounded_String (""),
+ Line => 1,
+ Col => 1);
end Peek;
-- peek2
@@ -28,11 +38,32 @@ package body Parser is
begin
if P.Pos + 1 < P.Tokens.Last_Index then
return P.Tokens (P.Pos + 1);
- else
- return Token'(Kind => EOF, Lexeme => To_Unbounded_String (""), Line => 1, Col => 1);
end if;
+ return
+ Token'
+ (Kind => EOF,
+ Lexeme => To_Unbounded_String (""),
+ Line => 1,
+ Col => 1);
end Peek2;
+ -- match
+
+ function Match (P : in Parser; Kind : Token_Kind) return Token is
+ T : Token;
+ begin
+ if P.Pos < P.Tokens.Last_Index then
+ T := P.Tokens (P.Pos);
+ if T.Kind = Kind then
+ return T;
+ else
+ raise Program_Error
+ with "expected different token: " & Token_Kind'Image (Kind);
+ end if;
+ end if;
+ raise Program_Error with "Match: Token out of bounds";
+ end Match;
+
-- consume
function Consume (P : in out Parser) return Token is
@@ -42,9 +73,8 @@ package body Parser is
T := P.Tokens (P.Pos);
P.Pos := P.Pos + 1;
return T;
- else
- return Token'(Kind => EOF, Lexeme => To_Unbounded_String (""), Line => 1, Col => 1);
end if;
+ raise Program_Error with "Consume: Token out of bounds";
end Consume;
-- parse program