summary refs log tree commit diff
path: root/bin
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
commit2901b90df2243e7844106cff525967b1d3305a8b (patch)
treee5bf4770e749599d47aab7ad11d2bd3b9917881d /bin
parentClean up hilex code somewhat (diff)
downloadsrc-2901b90df2243e7844106cff525967b1d3305a8b.tar.gz
src-2901b90df2243e7844106cff525967b1d3305a8b.zip
Clean up C lexer
This ordering of rules feels much cleaner.
Diffstat (limited to 'bin')
-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 1c703814..55f50f09 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; }