Skip to content

Commit 068c3dd

Browse files
committed
lexers: Add a Makefile lexer
1 parent 04942d7 commit 068c3dd

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

elixir/lexers/__main__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
lexer = lexers.KconfigLexer(f.read())
2020
elif filename.endswith(('.s', '.S')):
2121
lexer = lexers.GasLexer(f.read())
22+
elif filename.endswith('Makefile'):
23+
lexer = lexers.MakefileLexer(f.read())
2224
else:
2325
raise Exception("no lexer for filetype")
2426

elixir/lexers/lexers.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,3 +362,34 @@ def lex(self):
362362

363363
return simple_lexer(rules, self.code)
364364

365+
366+
# https://www.gnu.org/software/make/manual/make.html
367+
class MakefileLexer:
368+
# https://pubs.opengroup.org/onlinepubs/007904975/utilities/make.html
369+
370+
# NOTE same as in KConfig, we only care about screaming case names
371+
make_identifier = r'[A-Z0-9_]+'
372+
make_minor_identifier = r'[a-zA-Z0-9_][a-zA-Z0-9-_]*'
373+
make_variable = r'(\$\([a-zA-Z0-9_-]\)|\$\{[a-zA-Z0-9_-]\})'
374+
make_single_quote_string = r"'*?'"
375+
make_string = f'(({ make_single_quote_string })|({ shared.double_quote_string_with_escapes }))'
376+
make_escape = r'\\[#"\']'
377+
make_punctuation = r'[~\\`\[\](){}<>.,:;|%$^@&?!+*/=-]'
378+
make_comment = r'(?<!\\)#(\\\s*\n|[^\n])*\n'
379+
380+
rules = [
381+
(shared.whitespace, TokenType.WHITESPACE),
382+
(make_escape, TokenType.PUNCTUATION),
383+
(make_comment, TokenType.COMMENT),
384+
(make_string, TokenType.STRING),
385+
(make_identifier, TokenType.IDENTIFIER),
386+
(make_minor_identifier, TokenType.SPECIAL),
387+
(make_punctuation, TokenType.PUNCTUATION),
388+
]
389+
390+
def __init__(self, code):
391+
self.code = code
392+
393+
def lex(self):
394+
return simple_lexer(self.rules, self.code)
395+

0 commit comments

Comments
 (0)