#! @file regex.sakura #! @author John Christman (sorakatadzuma@gmail.com) #! @copyright Malunal Studios, LLC. #! @license MIT #! #! Contains the beginning of regular expressions: `regex::compile`, and the #! kinds of node a pattern is parsed into. #! #! It is not finished: a pattern is not yet parsed, and `compile` gives `null`. module std::text; #! Regular expressions, which `regex::compile` makes of a pattern. #! @remarks Not finished: a pattern is not yet parsed. pub struct regex { } #! Compiles a pattern into the graph of nodes that matches it. #! @param pattern The pattern to compile. #! @param alloc The optional allocator the nodes are held in. #! @returns The first node of the graph. #! @remarks Not finished: it gives `null`. pub def regex::compile( pattern: strview, alloc: maybe<&mut allocator> ): *regex::node { # Create tree, convert to jump table. var parser := regex::parser::new(alloc); val ntree := parser.parse(pattern); return null; } #! A node of the graph a pattern compiles into, which is one kind of node from #! `regex::nodes`. pub union regex::node { #! Text matched as it is written. pub var literal: regex::nodes::literal; #! A position matched without consuming text, as the start of a line. pub var anchor: regex::nodes::anchor; #! What must, or must not, follow. pub var lookahead: regex::nodes::lookahead; #! What must, or must not, come before. pub var lookbehind: regex::nodes::lookbehind; #! One of several alternatives. pub var choiceof: regex::nodes::choiceof; #! Its child, matched exactly once. pub var onlyone: regex::nodes::onlyone; #! Its child, matched once or not at all. pub var optionally: regex::nodes::optionally; #! Its child, matched any number of times. pub var zeroplus: regex::nodes::zeroplus; #! Its child, matched at least once. pub var oneplus: regex::nodes::oneplus; #! Its child, matched a number of times in a range. pub var repeat: regex::nodes::repeat; #! A group whose settings hold only within it. pub var local: regex::nodes::local; #! A group whose match is kept, to give or refer back to. pub var capture: regex::nodes::capture; #! What a capture matched, matched again. pub var reference: regex::nodes::reference; } #! The kinds of node a pattern compiles into, which a `regex::node` is one of. pub struct regex::nodes { } #! Text matched as it is written. pub struct regex::nodes::literal { } #! A position matched without consuming text, as the start or end of a line. pub struct regex::nodes::anchor { } #! What must, or must not, follow the position, which is not consumed. pub struct regex::nodes::lookahead { pub var negative: bool; #! Whether what follows must not match. } #! What must, or must not, come before the position. pub struct regex::nodes::lookbehind { pub var negative: bool; #! Whether what comes before must not match. } #! One of several alternatives, tried in order. pub struct regex::nodes::choiceof { } #! Its child, matched exactly once. pub struct regex::nodes::onlyone { pub var child: *mut regex::node; #! The node matched. pub var lazy: bool; #! Whether as little as possible is matched. } #! Its child, matched once or not at all, as `?` writes. pub struct regex::nodes::optionally { pub var child: *mut regex::node; #! The node matched. pub var lazy: bool; #! Whether as little as possible is matched. } #! Its child, matched any number of times, as `*` writes. pub struct regex::nodes::zeroplus { pub var child: *mut regex::node; #! The node matched. pub var lazy: bool; #! Whether as little as possible is matched. } #! Its child, matched at least once, as `+` writes. pub struct regex::nodes::oneplus { pub var child: *mut regex::node; #! The node matched. pub var lazy: bool; #! Whether as little as possible is matched. } #! Its child, matched a number of times in a range, as `{2,5}` writes. pub struct regex::nodes::repeat { pub var child: *mut regex::node; #! The node matched. pub var lazy: bool; #! Whether as little as possible is matched. } #! A group whose settings hold only within it. pub struct regex::nodes::local { pub var child: *mut regex::node; #! The node matched. pub var lazy: bool; #! Whether as little as possible is matched. } #! A group whose match is kept, to give or refer back to. pub struct regex::nodes::capture { } #! What a capture matched, matched again. pub struct regex::nodes::reference { } struct regex::parser { var allocator: maybe<&mut allocator>; } def regex::parser::new(alloc: maybe<&mut allocator>): regex::parser { return regex::parser { allocator = alloc }; } def regex::parser::parse(&mut this, pattern: strview): *mut regex::node { return null; } # pub def regex::builder( # alloc: maybe<&mut allocator> # ): regex_builder { # return regex_builder::new(alloc); # } # Adds a literal to the current builder representation. # pub def regex::builder::literal(&mut this, pattern: strview): # &mut regex::builder # { # val made := make_node(this.allocator); # made.kind = regex::node_kind::literal; # made.children = chain::<*regex::node>::new(this.allocator); # made.pattern = pattern; # this.children.append(®ex::builder { # allocator = this.allocator, # children = chain::::new(this.allocator), # node = made # }); # return this.children.last().some; # } # struct regex::builder { # var allocator: maybe<&mut allocator>; # var children: chain; # var node: *mut regex::node; # } # def make_node(alloc: maybe<&mut allocator>): *mut regex::node { # const wanted := size_of::(); # return match alloc { # some(held) => held.acquire(wanted), # none => raw_allocate(wanted) # } as *mut regex::node; # }