Skip to content

Commit

Permalink
add parser impl
Browse files Browse the repository at this point in the history
  • Loading branch information
rajikak committed Jan 14, 2024
1 parent 4def1b5 commit 4f0735e
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 29 deletions.
31 changes: 15 additions & 16 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,39 @@ const std = @import("std");
const lex = @import("lexer.zig");
const seg = @import("segment.zig");
const lib = @import("lib.zig");
const parser = @import("parser.zig");
const allocator = std.heap.page_allocator;

const SegmentType = seg.SegmentType;
const Segment = seg.Segment;
const LexerOptions = lex.LexerOptions;
const Lexer = lex.Lexer;
const print_buffer = lex.print_buffer;

const Token = lex.Token;

pub fn main() !void {
const allocator = std.heap.page_allocator;
var buffer = std.ArrayList(Token).init(allocator);
defer buffer.deinit();
const Parser = parser.Parser;

pub fn main() !void {
// use flags
const use_default = true;
const use_file = false;
const file = "assets/x12.base.one.txt";

const s1 = "GS*SH*4405197800*999999999*20111206*1045*00*X*004060";

const file = "../assets/x12.base.one.txt";
const s2 = try lib.readfile(file, allocator);

var ele_sep: u8 = '_';
var seg_sep: u8 = '^';
if (use_default) {
ele_sep = lex.default_element_sep;
seg_sep = lex.default_segment_sep;
}

var options = LexerOptions.init(lex.default_segment_sep, ele_sep);
if (use_file) {
const s = try lib.read_file(file, allocator);
var lexer = Lexer.init(s, 0, 0, false, options);
lexer.tokens(&buffer);
print_buffer(s, buffer);
const p = Parser.init(s2, ele_sep, seg_sep);
p.parse();
} else {
const s = "GS*SH*4405197800*999999999*20111206*1045*00*X*004060";
var lexer = Lexer.init(s, 0, 0, false, options);
lexer.tokens(&buffer);
print_buffer(s, buffer);
const p = Parser.init(s1, ele_sep, seg_sep);
p.parse();
}
}
32 changes: 32 additions & 0 deletions src/parser.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
const std = @import("std");

const lex = @import("lexer.zig");
const seg = @import("segment.zig");
const lib = @import("lib.zig");
const x12 = @import("x12.zig");

const SegmentType = seg.SegmentType;
const Segment = seg.Segment;
const LexerOptions = lex.LexerOptions;
const Lexer = lex.Lexer;

const Token = lex.Token;

// parse and produce an x12 document from an EDI stream
pub const Parser = struct {
s: []const u8,
options: LexerOptions,

pub fn init(s: []const u8, ele_sep: u8, seg_sep: u8) Parser {
var options = LexerOptions.init(seg_sep, ele_sep);
return Parser{ .s = s, .options = options };
}

pub fn parse(self: Parser) void {
var lexer = Lexer.init(self.s, self.options);
lexer.tokens();
lexer.pbuffer();
}
};

// implement tests for using parser from a string and file
26 changes: 13 additions & 13 deletions src/x12.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,44 @@ const Segment = seg.Segment;

const Token = tok.Token;

pub const transaction_set = struct {
pub const TransactionSet = struct {
header: Segment,
trailer: Segment,
segments: std.ArrayList(Segment),

pub fn init() transaction_set {
return transaction_set{
pub fn init() TransactionSet {
return TransactionSet{
.header = Segment.init(),
.trailer = Segment.init(),
.segments = std.ArrayList(Segment).init(),
};
}
};

pub const functional_grp = struct {
pub const FunctionalGroup = struct {
header: Segment,
trailer: Segment,
transactions: std.ArrayList(transaction_set),
transactions: std.ArrayList(TransactionSet),

pub fn init() functional_grp {
return functional_grp{
pub fn init() FunctionalGroup {
return FunctionalGroup{
.header = Segment.init(),
.trailer = Segment.init(),
.transactions = std.ArrayList(transaction_set).init(),
.transactions = std.ArrayList(TransactionSet).init(),
};
}
};

pub const interchange_ctrl = struct {
pub const InterchangeCtrl = struct {
header: Segment,
trailer: Segment,
fun_grps: std.ArrayList(functional_grp),
fun_grps: std.ArrayList(FunctionalGroup),

pub fn init() interchange_ctrl {
return interchange_ctrl{
pub fn init() InterchangeCtrl {
return InterchangeCtrl{
.header = Segment.init(),
.trailer = Segment.init(),
.fun_grps = std.ArrayList(functional_grp).init(),
.fun_grps = std.ArrayList(FunctionalGroup).init(),
};
}
};
Expand Down

0 comments on commit 4f0735e

Please sign in to comment.