Skip to content

Commit

Permalink
Started on preprocessor refactor and adding better tests
Browse files Browse the repository at this point in the history
  • Loading branch information
newcomb-luke committed Jul 26, 2024
1 parent f9b8c2d commit 0534821
Show file tree
Hide file tree
Showing 18 changed files with 4,592 additions and 4,136 deletions.
189 changes: 189 additions & 0 deletions docs/grammar.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
## Preprocessor Grammar

### Program

The root node of the entire parse tree

```
<program> ::= <PAST Node> { <PAST Node> }
```

### PAST Node

Preprocessor Abstract Syntax Tree node

```
<PAST Node> ::= <Inert Tokens>
| <Include>
| <Single-Line Macro Def>
| <Multi-Line Macro Def>
| <Macro Invocation>
| <Repeat>
| <If Statement>
| <Single-Line Macro Undef>
| <Multi-Line Macro Undef>
```

### Inert Tokens
```
<Inert Tokens> ::= <Inert Tokens> <Inert Tokens>
| <Instruction Mnemonic>
| <Operator>
| <Keyword>
| <Type>
| <Label>
| <InnerLabel>
| <InnerLabelReference>
| <Literal>
| <Whitespace>
| <Symbol>
```

### Include
```
<Include> ::= .include <Whitespace> <StringLiteral> <Newline>
| .include <Whitespace> <Macro Invocation> <Newline>
```

### Single-Line Macro Definition
```
<Single-Line Macro Def> ::= .define <Identifier> <Newline>
| .define <Identifier> <SLMacro Def Contents> <Newline>
| .define <Identifier> (<SLMacro Def Arguments>) <Newline>
| .define <Identifier> (<SLMacro Def Arguments>) <SLMacro Def Contents> <Newline>
```

### Single-Line Macro Definition Arguments
```
<SLMacro Def Arguments> ::= <Identifier> | {, <Identifier>}
```

### Single-Line Macro Definition Contents
```
<SLMacro Def Contents> ::= { <Identifier>
| <Literal>
| <Operator>
| <Keyword>
| <Type>
| <Label>
| <InnerLabel>
| <InnerLabelReference>
| <Symbol>
| <Macro Invocation> }
```

### Multi-Line Macro Definition
```
<Multi-Line Macro Def> ::= .macro <Identifier> <MLMacro Def Args> <Newline> .endmacro
| .macro <Identifier> <MLMacro Def Args> <Newline> <Multi-Line Macro Contents> .endmacro
```

### Multi-Line Macro Definition Arguments
```
<MLMacro Def Args> ::= <Literal Integer>
| <Literal Integer>-<Literal Integer> <MLMacor Def Defaults>
```

### Multi-Line Macro Definition Argument Defaults
```
<MLMacro Def Defaults> ::= <MLMacro Def Default> {, <MLMacro Def Default>}
```

### Multi-Line Macro Definition Argument Default
```
<MLMacro Def Default> ::= <MLMacro Def Default> <MLMacro Def Default>
| <Instruction Mnemonic>
| <Operator>
| <Keyword>
| <Type>
| <Label>
| <InnerLabel>
| <InnerLabelReference>
| <Literal>
| <Non-Newline Whitespace>
| <Non-Comma Symbol>
```

### Multi-Line Macro Definition Contents
```
<Multi-Line Macro Contents> ::= <PAST Node> | <MLMacro Arg Reference>
{ <PAST Node> | <MLMacro Arg Reference> }
```

### Macro Invocation
```
<Macro Invocation> ::= <Identifier>
| <Identifier> ()
| <Identifier> (<Macro Invocation Arguments>)
```

### Macro Invocation Arguments
```
<Macro Invocation Arguments> ::= <Macro Invocation Arg> {, <Macro Invocation Arg>}
```

### Macro Invocation Argument
```
<Macro Invocation Arg> ::= <Instruction Mnemonic>
| <Macro Invocation>
| <MLMacro Arg Reference>
| <Operator>
| <Keyword>
| <Type>
| <Label>
| <InnerLabel>
| <InnerLabelReference>
| <Literal>
| <Non-Newline Whitespace>
| <Non-Comma Symbol>
```

### Repeat
```
<Repeat> ::= .rep <Expression> <Newline> .endrep
| .rep <Expression> <Newline> <Repeat Contents> .endrep
```

### Repeat Contents
```
<Repeat Contents> ::= <PASTNode> { <PASTNode> }
```

### If Statement
```
<If Statement> ::= <If Begin> <If Contents> { <Middle If> <If Contents> } .endif
| <If Begin> <If Contents> { <Middle If> <If Contents> } .else <If Contents> .endif
```

### If Statement Beginning
```
<If Begin> ::= <Starting If> <Expression> <Newline>
```

### If Statement Starting Directive
```
<Starting If> ::= .if .ifn .ifdef .ifndef
```

### If Statement Middle Directive
```
<Middle If> ::= .elif .elifn .elifdef .elifndef
```

### If Statement Contents
```
<If Contents> ::= <PASTNode> { <PASTNode> }
```

### Single-Line Macro Undefinition
```
<Single-Line Macro Undef> ::= .undef <Identifier>
| .undef <Identifier> <Literal Integer>
```

### Multi-Line Macro Undefinition
```
<Multi-Line Macro Undef> ::= .unmacro <Identifier>
| .unmacro <Identifier> <Literal Integer>
| .unmacro <Identifier> <Literal Integer>-<Literal Integer>
```
12 changes: 10 additions & 2 deletions src/errors/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -484,8 +484,11 @@ impl Handler {

/// This registers an error with this error Handler
pub fn error(&self, error: Diagnostic) {
if let Ok(inner) = self.inner.lock() {
inner.emitter.emit_diagnostic(&error);
// If we can't even emit them, don't even store them
if !self.flags.quiet {
if let Ok(inner) = self.inner.lock() {
inner.emitter.emit_diagnostic(&error);
}
}
}
}
Expand Down Expand Up @@ -687,6 +690,11 @@ impl Snippet {
pub fn as_slice(&self) -> &str {
&self.line[self.start_col..self.end_col]
}

pub fn as_string(self) -> String {
// Some optimization could be done here
self.as_slice().to_string()
}
}

impl Display for Snippet {
Expand Down
85 changes: 0 additions & 85 deletions src/lexer/errors.rs

This file was deleted.

Loading

0 comments on commit 0534821

Please sign in to comment.