From 2901b90df2243e7844106cff525967b1d3305a8b Mon Sep 17 00:00:00 2001 From: "C. McEnroe" Date: Tue, 29 Dec 2020 18:37:17 -0500 Subject: Clean up C lexer This ordering of rules feels much cleaner. --- bin/hilex/c.l | 85 +++++++++++++++++++++++++++++------------------------------ 1 file changed, 42 insertions(+), 43 deletions(-) (limited to 'bin/hilex') 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; } -"<"[^>]+">" { return String; } - -{ - {ident}/"(" { - BEGIN(pop = MacroLine); - return IdentifierTag; - } - {ident} { - BEGIN(pop = MacroLine); - return Macro; - } -} - -{ - "\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; } -{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; +} + +"<"[^>]+">" { + return String; +} +{ident}/"(" { + BEGIN(pop = MacroLine); + return IdentifierTag; +} +{ident} { + BEGIN(pop = MacroLine); + return Macro; +} + +{ + "\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; } -. { return Macro; } +. { + return Macro; +} .|\n { return Normal; } -- cgit 1.4.1