commit 1d66951010ff3740fbd8b0dc9ba271514f6b816d
parent 3f7861cf98647b845043253ed37df0954c8fa42e
Author: citbl <citbl@citbl.org>
Date: Thu, 4 Jun 2026 20:12:23 +1000
parser wip
Diffstat:
5 files changed, 77 insertions(+), 19 deletions(-)
diff --git a/src/lexer.adb b/src/lexer.adb
@@ -4,13 +4,11 @@ with Ada.Characters.Handling; use Ada.Characters.Handling;
package body Lexer is
- function Init (File_Name : String; File_Contents : String) return Lexer is
+ function Init (File_Name : File_Name_Ref; File_Contents : Source_Ref) return Lexer is
L : Lexer :=
(Source => File_Contents,
- Src_Len => File_Contents'Length,
File_Name => File_Name,
- FN_Len => File_Name'Length,
- Tokens => Token_Vectors.Empty_Vector,
+ Tokens => Token_Vector.Empty_Vector,
Pos => 1,
Line => 1,
Col => 1);
diff --git a/src/lexer.ads b/src/lexer.ads
@@ -2,6 +2,10 @@ with Ada.Containers.Vectors;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
package Lexer is
+
+ type Source_Ref is not null access constant String;
+ type File_Name_Ref is not null access constant String;
+
type Token_Kind is
(Ident,
Float_Literal,
@@ -27,22 +31,20 @@ package Lexer is
Line, Col : Positive;
end record;
- package Token_Vectors is new
+ package Token_Vector is new
Ada.Containers.Vectors (Index_Type => Natural, Element_Type => Token);
- type Lexer
- (Src_Len : Positive;
- FN_Len : Positive)
- is record
- Source : String (1 .. Src_Len);
- File_Name : String (1 .. FN_Len);
- Tokens : Token_Vectors.Vector;
+ type Lexer is record
+ Source : Source_Ref;
+ File_Name : File_Name_Ref;
+ Tokens : Token_Vector.Vector;
Pos : Positive;
Line : Positive;
Col : Positive;
end record;
- function Init (File_Name : String; File_Contents : String) return Lexer;
+ function Init
+ (File_Name : File_Name_Ref; File_Contents : Source_Ref) return Lexer;
procedure Add_Token (L : in out Lexer; T : Token);
procedure Print_Tokens (L : in out Lexer);
diff --git a/src/nightshade.adb b/src/nightshade.adb
@@ -1,9 +1,10 @@
with Ada.Text_IO; use Ada.Text_IO;
-with Utils; use Utils;
-with Lexer;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
with Ada.Strings.Unbounded.Text_IO; use Ada.Strings.Unbounded.Text_IO;
with Ada.Command_Line; use Ada.Command_Line;
+with Utils;
+with Lexer;
+with Parser;
procedure Nightshade is
begin
@@ -14,12 +15,20 @@ begin
declare
File_Name : constant String := Argument (1);
- File_Contents : constant String := Utils.Load_Contents (File_Name);
- L : Lexer.Lexer := Lexer.Init (File_Name, File_Contents);
+ Buffer : constant String := Utils.Load_Contents (File_Name);
+ Src_Ref : constant Lexer.Source_Ref := new String'(Buffer);
+ File_Name_Ref : constant Lexer.File_Name_Ref := new String'(File_Name);
+ L : Lexer.Lexer := Lexer.Init (File_Name_Ref, Src_Ref);
begin
Lexer.Lex (L);
Lexer.Print_Tokens (L);
- Put_Line ("");
- Put_Line ("done");
+ Put_Line ("done lexing");
+
+ declare
+ P : Parser.Parser := Parser.Init (File_Name_Ref, Src_Ref);
+ begin
+ Parser.Parse(P, L.Tokens);
+ Parser.Print_AST (P);
+ end;
end;
end Nightshade;
diff --git a/src/parser.adb b/src/parser.adb
@@ -0,0 +1,33 @@
+with Lexer; use Lexer;
+
+package body Parser is
+ function Init
+ (File_Name : File_Name_Ref; Source : Source_Ref)
+ return Parser
+ is
+ P : Parser :=
+ (Source => Source, File_Name => File_Name, Pos => 1);
+ begin
+ return P;
+ end Init;
+
+ procedure Parse_Program (P : in out Parser; Next_Token : Token) is
+ begin
+ if Next_Token =
+ end Parse_Program;
+
+ procedure Parse (P : in out Parser; Tokens : Token_Vector.Vector) is
+ Next_Token : Token := Tokens(P.Pos);
+ begin
+ while Next_Token /= EOF loop
+ Parse_Program(P, Next_Token);
+ end loop;
+ null;
+ end Parse;
+
+ procedure Print_AST (P : Parser) is
+ begin
+ null;
+ end Print_AST;
+
+end Parser;
diff --git a/src/parser.ads b/src/parser.ads
@@ -0,0 +1,16 @@
+with Ada.Containers.Vectors;
+with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
+with Lexer; use Lexer;
+
+package Parser is
+ type Parser is record
+ Source : Source_Ref;
+ File_Name : File_Name_Ref;
+ Pos : Positive;
+ end record;
+
+ function Init
+ (File_Name : File_Name_Ref; Source : Source_Ref) return Parser;
+ procedure Parse (P : in out Parser; Tokens : Lexer.Token_Vector.Vector);
+ procedure Print_AST (P : Parser);
+end Parser;