Skip to content

Commit

Permalink
Implement some handling of preprocessor macros
Browse files Browse the repository at this point in the history
  • Loading branch information
bbannier committed Sep 26, 2023
1 parent 9763f65 commit 1005acc
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions corpus/preprocessor.spicy
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Preprocessor macros with varying contents.

@if 1
# foo
@else

@endif

@if 1
1;

2;
@else

2;
@endif

@if ! 1
1;
@endif

@if 1 ==1

1;
@endif

# FIXME(bbannier): Empty lines in bodies are currently not removed.
@if

@else

@endif


# We allow constructs like `import` in preprocessor macros.
@if 1
import foo ;

@endif

# We allow field declarations in preprocessor macros.
type X = unit {
# FIXME(bbannier): preprocessor macros inherit indention, we might not want to do that.
@if 1
: bytes &size=1;
@endif
};
41 changes: 41 additions & 0 deletions corpus/preprocessor.spicy.expected
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Preprocessor macros with varying contents.

@if 1
# foo
@else
@endif

@if 1
1;
2;
@else
2;
@endif

@if !1
1;
@endif

@if 1 == 1
1;
@endif

# FIXME(bbannier): Empty lines in bodies are currently not removed.
@if

@else

@endif

# We allow constructs like `import` in preprocessor macros.
@if 1
import foo;
@endif

# We allow field declarations in preprocessor macros.
type X = unit {
# FIXME(bbannier): preprocessor macros inherit indention, we might not want to do that.
@if 1
: bytes &size=1;
@endif
};
25 changes: 25 additions & 0 deletions src/query.scm
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,28 @@
(_)
"{" @prepend_space
)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Preprocessor macros

; Empty lines before preprocessor blocks are fine.
(preproc) @allow_blank_line_before

; `@if` always has a following expression so it needs a space.
(preproc "@if" @append_space)

; Newline handling. This is slightly nasty since preprocessor macros are
; terminated by explicit newlines instead of `;`. Force a newline, but delete
; original newlines so we do not insert stray empty lines or spaces.
(preproc
"@if" (expression) @append_hardline
"\n" @delete
)
(preproc
"@else" @append_hardline
"\n" @delete
)
(preproc
"@endif" @prepend_hardline
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

0 comments on commit 1005acc

Please sign in to comment.