Skip to content

Commit

Permalink
Add support for nested bracket strings in self-hosted parser.
Browse files Browse the repository at this point in the history
  • Loading branch information
jemc committed Jun 17, 2023
1 parent 9fa9515 commit fe95afc
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
:actor Main
:new
<<<FOO>>>
<<<FOO<<<BAR>>>BAZ>>>
<<<
FOO
BAR
BAZ
>>>
<<<
FOO
<<<
BAR
>>>
BAZ
>>>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#1:
declares: #2:
0: #3:
terms: #4:
0: #5:
name: "actor"
1: #6:
name: "Main"
body: #7:
1: #8:
terms: #9:
0: #10:
name: "new"
body: #11:
terms: #12:
0: #13:
string: "FOO"
1: #14:
string: "FOO<<<BAR>>>BAZ"
2: #15:
string: "FOO\nBAR\nBAZ"
3: #16:
string: "FOO\n<<<\n BAR\n>>>\nBAZ"
14 changes: 7 additions & 7 deletions self-hosted/src/savi-lang-parse/_Grammar.savi
Original file line number Diff line number Diff line change
Expand Up @@ -92,16 +92,16 @@
character =
@char('\'') >> character_char.repeat.token(_Token.Character) >> @char('\'')

// Define what a heredoc string looks like.
heredoc_content = @declare
heredoc = @str("<<<") >> heredoc_content.token(_Token.HereDoc) >> @str(">>>")
heredoc_no_token = @str("<<<") >> heredoc_content >> @str(">>>")
heredoc_content.define(
(heredoc_no_token / (@str(">>>").not >> @any)).repeat
// Define what a nestable bracket_string string looks like.
bracket_string_content = @declare
bracket_string = @str("<<<") >> bracket_string_content.token(_Token.BracketString) >> @str(">>>")
bracket_string_no_token = @str("<<<") >> bracket_string_content >> @str(">>>")
bracket_string_content.define(
(bracket_string_no_token / (@str(">>>").not >> @any)).repeat
)

// Define an atom to be a single term with no binary operators.
anystring = string / character / heredoc
anystring = string / character / bracket_string
atom = parens / anystring / float / integer / ident

// Define a compound to be a closely bound chain of atoms.
Expand Down
37 changes: 37 additions & 0 deletions self-hosted/src/savi-lang-parse/_StringLiterals.savi
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
:module _StringLiterals
:fun process_bracket_string_indentation(string String) String
// Look at the first few characters of the string to find the indentation.
first_non_space_index USize = 0
saw_newline = False
string.each_char_with_index_and_width -> (char, index, width |
case char == (
| ' ' |
| '\t' |
| '\r' |
if saw_newline (
first_non_space_index = index
break string // TODO: no need for break value here
)
| '\n' |
if saw_newline (
first_non_space_index = index
break string // TODO: no need for break value here
)
saw_newline = True
|
first_non_space_index = index
break string // TODO: no need for break value here
)
)

// If the string has no indentation, or is only whitespace, return as-is.
return string if (first_non_space_index == 0 || !saw_newline)

// Remove the indentation and trim remaining whitespace before and after.
initial_indentation = string.trim(0, first_non_space_index)
content = string.clone.replace_all(initial_indentation, "\n")
content.trim_in_place(
content.leading_whitespace_index
content.trailing_whitespace_index
)
--content
2 changes: 1 addition & 1 deletion self-hosted/src/savi-lang-parse/_Token.savi
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
:member Character 7
:member String 8
:member PrefixedString 9
:member HereDoc 10
:member BracketString 10
:member Operator 11
:member Compound 12
:member Prefix 13
Expand Down
9 changes: 9 additions & 0 deletions self-hosted/src/savi-lang-parse/_TreeBuilder.savi
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,15 @@
@error.at(token, _Error.ToDoComposeString)
)

| _Token.BracketString |
if (children_count == 0) (
ast.init_string(
_StringLiterals.process_bracket_string_indentation(data.get_string(token))
)
|
@error.at(token, _Error.ToDoComposeString)
)

| _Token.PrefixedString |
try (
error! if (children_count != 2)
Expand Down

0 comments on commit fe95afc

Please sign in to comment.