hy/hy/lex/lexer.py

63 lines
1.7 KiB
Python
Raw Normal View History

2018-01-01 16:38:33 +01:00
# Copyright 2018 the authors.
# This file is part of Hy, which is free software licensed under the Expat
# license. See the LICENSE.
2013-04-08 09:56:04 +02:00
from rply import LexerGenerator
lg = LexerGenerator()
# A regexp for something that should end a quoting/unquoting operator
# i.e. a space or a closing brace/paren/curly
end_quote = r'(?![\s\)\]\}])'
identifier = r'[^()\[\]{}\'"\s;]+'
2013-04-08 09:56:04 +02:00
lg.add('LPAREN', r'\(')
lg.add('RPAREN', r'\)')
lg.add('LBRACKET', r'\[')
lg.add('RBRACKET', r'\]')
lg.add('LCURLY', r'\{')
lg.add('RCURLY', r'\}')
2015-06-26 23:47:35 +02:00
lg.add('HLCURLY', r'#\{')
2013-04-08 09:56:04 +02:00
lg.add('QUOTE', r'\'%s' % end_quote)
lg.add('QUASIQUOTE', r'`%s' % end_quote)
lg.add('UNQUOTESPLICE', r'~@%s' % end_quote)
lg.add('UNQUOTE', r'~%s' % end_quote)
2017-08-02 23:53:46 +02:00
lg.add('DISCARD', r'#_')
2017-07-17 22:34:39 +02:00
lg.add('HASHSTARS', r'#\*+')
lg.add('BRACKETSTRING', r'''(?x)
\# \[ ( [^\[\]]* ) \[ # Opening delimiter
\n? # A single leading newline will be ignored
((?:\n|.)*?) # Content of the string
\] \1 \] # Closing delimiter
''')
lg.add('HASHOTHER', r'#%s' % identifier)
2013-04-08 09:56:04 +02:00
# A regexp which matches incomplete strings, used to support
# multi-line strings in the interpreter
partial_string = r'''(?x)
2017-02-19 01:15:58 +01:00
(?:u|r|ur|ru|b|br|rb)? # prefix
2013-04-08 09:56:04 +02:00
" # start string
(?:
| [^"\\] # non-quote or backslash
| \\(.|\n) # or escaped single character or newline
2013-04-08 09:56:04 +02:00
| \\x[0-9a-fA-F]{2} # or escaped raw character
| \\u[0-9a-fA-F]{4} # or unicode escape
| \\U[0-9a-fA-F]{8} # or long unicode escape
)* # one or more times
'''
2013-04-08 09:56:04 +02:00
lg.add('STRING', r'%s"' % partial_string)
lg.add('PARTIAL_STRING', partial_string)
2013-04-08 09:56:04 +02:00
lg.add('IDENTIFIER', identifier)
2013-04-08 09:56:04 +02:00
lg.ignore(r';.*(?=\r|\n|$)')
2013-04-08 09:56:04 +02:00
lg.ignore(r'\s+')
lexer = lg.build()