summary refs log tree commit diff
path: root/bin/hilex
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-12-29 18:37:17 -0500
committerJune McEnroe <june@causal.agency>2020-12-29 18:37:17 -0500
commitbb373662b37f547b979bbf95c54c3d45864d4a8a (patch)
tree42dbbd1b9f63e695ad065f763704f6f5823a6981 /bin/hilex
parentClean up hilex code somewhat (diff)
downloadsrc-bb373662b37f547b979bbf95c54c3d45864d4a8a.tar.gz
src-bb373662b37f547b979bbf95c54c3d45864d4a8a.zip
Clean up C lexer
This ordering of rules feels much cleaner.
Diffstat (limited to '')
-rw-r--r--bin/hilex/c.l85
1 files changed, 42 insertions, 43 deletions
diff --git a/bin/hilex/c.l b/bin/hilex/c.l
index 985ff513..8c4a8ca1 100644
--- a/bin/hilex/c.l
+++ b/bin/hilex/c.l
@@ -30,45 +30,15 @@ width "*"|[0-9]+
 
 %%
 	static int pop = INITIAL;
-	static int braces = 0;
+	static int depth = 0;
 
 [[:blank:]]+ { return Normal; }
 
-^"#"[[:blank:]]*("include"|"import") {
-	BEGIN(pop = MacroInclude);
-	return Macro;
-}
-^"#"[[:blank:]]*"define" {
-	BEGIN(pop = MacroDefine);
-	return Macro;
-}
-^[#%][[:blank:]]*{ident} |
-^"%"[%{}] {
+^"%"[%{}]? {
 	BEGIN(pop = MacroLine);
 	return Macro;
 }
 
-<MacroInclude>"<"[^>]+">" { return String; }
-
-<MacroDefine>{
-	{ident}/"(" {
-		BEGIN(pop = MacroLine);
-		return IdentifierTag;
-	}
-	{ident} {
-		BEGIN(pop = MacroLine);
-		return Macro;
-	}
-}
-
-<MacroLine,MacroInclude,MacroDefine>{
-	"\n" {
-		BEGIN(pop = INITIAL);
-		return Normal;
-	}
-	"\\\n" { return Macro; }
-}
-
 ([-+*/%&|^=!<>]|"<<"|">>")"="? |
 [=~.?:]|"++"|"--"|"&&"|"||"|"->" |
 sizeof|(_A|alignof) {
@@ -101,12 +71,45 @@ register|restrict|return|static|struct|switch|typedef|union|volatile|while |
 	return Keyword;
 }
 
-<MacroLine>{ident} { return Macro; }
+^"#"[[:blank:]]*(include|import) {
+	BEGIN(pop = MacroInclude);
+	return Macro;
+}
+^"#"[[:blank:]]*define {
+	BEGIN(pop = MacroDefine);
+	return Macro;
+}
+^"#"[[:blank:]]*{ident} {
+	BEGIN(pop = MacroLine);
+	return Macro;
+}
+
+<MacroInclude>"<"[^>]+">" {
+	return String;
+}
+<MacroDefine>{ident}/"(" {
+	BEGIN(pop = MacroLine);
+	return IdentifierTag;
+}
+<MacroDefine>{ident} {
+	BEGIN(pop = MacroLine);
+	return Macro;
+}
+
+<MacroLine,MacroInclude,MacroDefine>{
+	"\n" {
+		BEGIN(pop = INITIAL);
+		return Normal;
+	}
+	"\\\n" { return Macro; }
+	{ident} { return Macro; }
+}
+
 {ident} { return Identifier; }
 
 {ident}/("["[^]]*"]")*[[:space:]]*"=" |
 {ident}/"("[^)]*")"[[:space:]]*"{" {
-	return (!braces ? IdentifierTag : Identifier);
+	return (!depth ? IdentifierTag : Identifier);
 }
 
 "//"([^\n]|"\\\n")* |
@@ -154,16 +157,12 @@ register|restrict|return|static|struct|switch|typedef|union|volatile|while |
 	[^%\\""]+|. { return String; }
 }
 
-"{" {
-	braces++;
-	REJECT;
-}
-"}" {
-	braces--;
-	REJECT;
-}
+"{" { depth++; REJECT; }
+"}" { depth--; REJECT; }
 
-<MacroLine,MacroInclude,MacroDefine>. { return Macro; }
+<MacroLine,MacroInclude,MacroDefine>. {
+	return Macro;
+}
 
 .|\n { return Normal; }