summary refs log tree commit diff
diff options
context:
space:
mode:
authorJune McEnroe <june@causal.agency>2020-12-28 22:09:13 -0500
committerJune McEnroe <june@causal.agency>2020-12-28 22:09:13 -0500
commit04dc80ccbf3ba015483f2237f2b4b5ddde6f35cb (patch)
treed7747b47528024ffbaa4696bd28c1a435bbcc361
parentSet H_SETUNIQUE (diff)
downloadsrc-04dc80ccbf3ba015483f2237f2b4b5ddde6f35cb.tar.gz
src-04dc80ccbf3ba015483f2237f2b4b5ddde6f35cb.zip
Lex strings inside macros
Diffstat (limited to '')
-rw-r--r--bin/hilex/c.l68
1 files changed, 36 insertions, 32 deletions
diff --git a/bin/hilex/c.l b/bin/hilex/c.l
index 159980e2..e208d76b 100644
--- a/bin/hilex/c.l
+++ b/bin/hilex/c.l
@@ -27,9 +27,15 @@
 width "*"|[0-9]+
 
 %%
+	static int pop = INITIAL;
 
 [[:space:]]+ { return Normal; }
 
+^"%" {
+	BEGIN(pop = MacroLine);
+	return Macro;
+}
+
 ([-+*/%&|^=!<>]|"<<"|">>")"="? |
 [=~.?:]|"++"|"--"|"&&"|"||"|"->" |
 sizeof|(_A|alignof) {
@@ -51,45 +57,22 @@ register|restrict|return|static|struct|switch|typedef|union|volatile|while |
 
 [_[:alpha:]][_[:alnum:]]* { return Identifier; }
 
-^"#" {
-	BEGIN(MacroLine);
-	return Macro;
-}
-^"#"[[:blank:]]*"include" {
-	BEGIN(MacroInclude);
-	return Macro;
-}
-<MacroLine,MacroInclude>{
-	"\n" {
-		BEGIN(0);
-		return Macro;
-	}
-	"\\\n" { return Macro; }
-	[^\\\n<"]+|. { return Macro; }
-}
-<MacroInclude>{
-	"<"[^>]+">" |
-	"\""[^"]+"\"" {
-		return String;
-	}
-}
-
-"//"([^\n]|"\\\n")* |
-"/*"([^*]|"*"[^/])*"*"+"/" {
+<INITIAL,MacroLine>"//"([^\n]|"\\\n")* |
+<INITIAL,MacroLine>"/*"([^*]|"*"[^/])*"*"+"/" {
 	return Comment;
 }
 
-[LUu]?"'" {
+<INITIAL,MacroLine>[LUu]?"'" {
 	BEGIN(CharLiteral);
 	return String;
 }
-([LU]|u8?)?"\"" {
+<INITIAL,MacroLine,MacroInclude>([LU]|u8?)?"\"" {
 	BEGIN(StringLiteral);
 	return String;
 }
 
 <CharLiteral,StringLiteral>{
-	"\\"['""?\\abfnrtv] |
+	"\\"[''""?\\abfnrtv] |
 	"\\"([0-7]{1,3}) |
 	"\\x"([[:xdigit:]]{2}) |
 	"\\u"([[:xdigit:]]{4}) |
@@ -106,17 +89,38 @@ register|restrict|return|static|struct|switch|typedef|union|volatile|while |
 
 <CharLiteral>{
 	"'" {
-		BEGIN(0);
+		BEGIN(pop);
 		return String;
 	}
-	[^\\']+|. { return String; }
+	[^\\'']+|. { return String; }
 }
 <StringLiteral>{
 	"\"" {
-		BEGIN(0);
+		BEGIN(pop);
 		return String;
 	}
-	[^%\\"]+|. { return String; }
+	[^%\\""]+|. { return String; }
+}
+
+^"#" {
+	BEGIN(pop = MacroLine);
+	return Macro;
+}
+^"#"[[:blank:]]*"include" {
+	BEGIN(pop = MacroInclude);
+	return Macro;
+}
+<MacroInclude>"<"[^>]+">" {
+	BEGIN(pop = MacroLine);
+	return String;
+}
+<MacroLine,MacroInclude>{
+	"\n" {
+		BEGIN(pop = INITIAL);
+		return Macro;
+	}
+	"\\\n" { return Macro; }
+	[^\\\n/<>''""]+|. { return Macro; }
 }
 
 . { return Normal; }