Skip to content

Commit

Permalink
feat: Changed patterns delimiter _pattern_ to $"pattern"
Browse files Browse the repository at this point in the history
closes #165
  • Loading branch information
giann committed Aug 24, 2023
1 parent f3c62f8 commit 33eec4a
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/parser.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3344,7 +3344,7 @@ pub const Parser = struct {

const source_slice = self.parser.previous_token.?.literal_string.?;
// Replace escaped pattern delimiter with delimiter
const source_slice_clean = try std.mem.replaceOwned(u8, self.gc.allocator, source_slice, "__", "_");
const source_slice_clean = try std.mem.replaceOwned(u8, self.gc.allocator, source_slice, "\\\"", "\"");
const source = try self.gc.allocator.dupeZ(u8, source_slice_clean);

var err = try self.gc.allocator.allocSentinel(u8, 1000, 0);
Expand Down
12 changes: 8 additions & 4 deletions src/scanner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ pub const Scanner = struct {
'`' => self.string(true),
'@' => self.atIdentifier(),
'|' => self.docblock(),
'_' => self.pattern(),
'$' => self.pattern(),

else => self.makeToken(.Error, "Unexpected character.", null, null),
};
Expand Down Expand Up @@ -307,10 +307,14 @@ pub const Scanner = struct {
}

fn pattern(self: *Self) !Token {
while ((self.peek() != '_' or self.peekNext() == '_') and !self.isEOF()) {
if (self.advance() != '"') {
return self.makeToken(.Error, "Unterminated pattern.", null, null);
}

while ((self.peek() != '"' or self.peekNext() == '"') and !self.isEOF()) {
if (self.peek() == '\n') {
return self.makeToken(.Error, "Unterminated pattern.", null, null);
} else if (self.peek() == '_' and self.peekNext() == '_') {
} else if (self.peek() == '\\' and self.peekNext() == '"') {
// Escaped pattern delimiter, go past it
_ = self.advance();
}
Expand All @@ -327,7 +331,7 @@ pub const Scanner = struct {
return self.makeToken(
.Pattern,
if (self.current.offset - self.current.start > 0)
self.source[(self.current.start + 1)..(self.current.offset - 1)]
self.source[(self.current.start + 2)..(self.current.offset - 1)]
else
null,
null,
Expand Down
2 changes: 1 addition & 1 deletion tests/032-debug.buzz
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,5 @@ test "dump" {
debug.dump(MyStringEnum.One);
debug.dump(MyEnum.One);
debug.dump("hello world");
debug.dump(_hello .*_);
debug.dump($"hello .*");
}
12 changes: 6 additions & 6 deletions tests/036-pattern.buzz
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import "std";

test "pattern.match" {
pat pattern = _hello ([a-z]+)_;
pat pattern = $"hello ([a-z]+)";

[str]? results = pattern.match("All i want to say is hello joe! hello mundo!");

Expand All @@ -11,7 +11,7 @@ test "pattern.match" {
}

test "pattern.matchAll" {
pat pattern = _hello ([a-z]+)_;
pat pattern = $"hello ([a-z]+)";

[[str]]? results = pattern.matchAll("All i want to say is hello joe!\nhello mundo!\nAnd hello neighbor...");

Expand All @@ -21,19 +21,19 @@ test "pattern.matchAll" {
}

test "Escaped pattern delimiter" {
pat pattern = _hello __ world_;
pat pattern = $"hello \" world";

assert("{pattern}" == "hello _ world", message: "delimiter was properly escaped");
assert("{pattern}" == "hello \" world", message: "delimiter was properly escaped");
}

test "replace" {
assert(
_world_.replace("All i want to say is hello world!", with: "mundo") == "All i want to say is hello mundo!",
$"world".replace("All i want to say is hello world!", with: "mundo") == "All i want to say is hello mundo!",
message: "replace"
);

assert(
_alright_.replaceAll("he says: alright, alright, alright!", with: "check") == "he says: check, check, check!",
$"alright".replaceAll("he says: alright, alright, alright!", with: "check") == "he says: check, check, check!",
message: "replaceAll"
);
}
2 changes: 1 addition & 1 deletion tests/059-types-as-value.buzz
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ test "typeof" {
assert((typeof null) == <void>, message: "typeof operator");
assert((typeof 1) == <int>, message: "typeof operator");
assert((typeof 3.14) == <float>, message: "typeof operator");
assert((typeof _hello_) == <pat>, message: "typeof operator");
assert((typeof $"hello") == <pat>, message: "typeof operator");
assert((typeof dumpType) == <Function(type myType) > void > void>, message: "typeof operator");
}

Expand Down
4 changes: 2 additions & 2 deletions tools/gendoc.buzz
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ object Declaration {

| Split docblock by @ instructions
static fun handleDocblock(str docblock) > {str, str} {
const pat atPattern = _@([^@ ]+)(\s+[^\n@]+)?_;
const pat atPattern = $"@([^@ ]+)(\s+[^\n@]+)?";
{str, str} out = {<str, str>};

foreach (str line in docblock.split("\\n")) {
Expand All @@ -47,7 +47,7 @@ object Declaration {
}

fun toMarkdown(str? heading, Buffer out) > void !> WriteWhileReadingError {
const pat atParamPattern = _([^\s]+)\s+(.+)_;
const pat atParamPattern = $"([^\s]+)\s+(.+)";

if (this.docblock == null) {
return;
Expand Down
4 changes: 2 additions & 2 deletions tools/lsp.buzz
Original file line number Diff line number Diff line change
Expand Up @@ -877,7 +877,7 @@ fun getSymbolsAt(Boxed node, [Boxed] declarations) > void {
}

if (ndType == "FunDeclaration") {
pat namePattern = _fun (.+)\(_;
pat namePattern = $"fun (.+)\(";
str name = namePattern.match(node.q(["type_def"]).stringValue())![1];

declarations.append(Boxed{
Expand All @@ -893,7 +893,7 @@ fun getSymbolsAt(Boxed node, [Boxed] declarations) > void {
}

if (ndType == "ObjectDeclaration") {
pat namePattern = _(object) (.+)_;
pat namePattern = $"(object) (.+)";
[str] match = namePattern.match(node.q(["type_def"]).stringValue())!;
SymbolKind kind = SymbolKind.Object;
str name = match[2];
Expand Down

0 comments on commit 33eec4a

Please sign in to comment.