summaryrefslogtreecommitdiff
path: root/src/tokens/semantic.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/tokens/semantic.rs')
-rw-r--r--src/tokens/semantic.rs90
1 files changed, 90 insertions, 0 deletions
diff --git a/src/tokens/semantic.rs b/src/tokens/semantic.rs
new file mode 100644
index 0000000..ac5179c
--- /dev/null
+++ b/src/tokens/semantic.rs
@@ -0,0 +1,90 @@
+use crate::*;
+
+use SemanticTokenVariant as SemVar;
+
+
+pub struct SemanticToken {
+ pub source: SourceSpan,
+ pub bytecode: BytecodeSpan,
+ pub variant: SemanticTokenVariant,
+}
+
+
+pub enum SemanticTokenVariant {
+ LabelDefinition(LabelDefinition),
+ MacroDefinition(MacroDefinition),
+
+ /// Pointer to the matching label definition.
+ LabelReference(usize),
+ /// Pointer to the matching macro definition.
+ MacroInvocation(usize),
+
+ Literal(Value),
+ Padding(Value),
+ Instruction(Instruction),
+
+ Comment(String),
+ String(Vec<u8>),
+
+ /// Pointer to the matching block close.
+ BlockOpen(usize),
+ /// Pointer to the matching block open.
+ BlockClose(usize),
+ MarkOpen,
+ MarkClose,
+
+ Error(SemanticParseError),
+}
+
+impl std::fmt::Debug for SemanticToken {
+ fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
+ match &self.variant {
+ SemVar::LabelDefinition(def) => write!(f, "LabelDefinition({})", def.name),
+ SemVar::MacroDefinition(def) => write!(f, "MacroDefinition({})", def.name),
+ SemVar::LabelReference(pointer) => write!(f, "LabelReference(*{pointer})"),
+ SemVar::MacroInvocation(pointer) => write!(f, "MacroInvocation(*{pointer})"),
+ SemVar::Literal(value) => write!(f, "Literal({value})"),
+ SemVar::Padding(value) => write!(f, "Padding({value})"),
+ SemVar::Instruction(instr) => write!(f, "Instruction(0x{:02x})", instr.value),
+ SemVar::Comment(comment) => write!(f, "Comment({comment})"),
+ SemVar::String(string) => write!(f, "String({})", String::from_utf8_lossy(&string)),
+ SemVar::BlockOpen(_) => write!(f, "BlockOpen"),
+ SemVar::BlockClose(_) => write!(f, "BlockClose"),
+ SemVar::MarkOpen => write!(f, "MarkOpen"),
+ SemVar::MarkClose => write!(f, "MarkClose"),
+ SemVar::Error(_) => write!(f, "Error"),
+ }
+ }
+}
+
+
+pub struct LabelDefinition {
+ /// The absolute name of the label or sublabel.
+ pub name: String,
+ /// List of pointers to label reference tokens.
+ pub references: Vec<usize>,
+}
+
+
+pub struct MacroDefinition {
+ pub name: String,
+ pub references: Vec<usize>,
+ pub body_tokens: Vec<SemanticToken>,
+}
+
+
+pub enum SemanticParseError {
+ LabelDefinitionInMacroDefinition,
+ MacroDefinitionInMacroDefinition,
+
+ StrayMacroTerminator,
+ StrayBlockClose,
+ UnclosedBlock,
+
+ UndefinedSymbol(String),
+ RedefinedSymbol((String, SourceSpan)),
+
+ MacroInvocationBeforeDefinition((String, SourceSpan)),
+
+ SyntaxError(SyntacticParseError)
+}