127 lines
3.1 KiB
Python
127 lines
3.1 KiB
Python
## (c) 2018 Jan Lerking
|
|
## Python lexer for c header files.
|
|
## Used for creating corresponding NASM include files.
|
|
## Contains tokens expanded with reserved 'C' keywords
|
|
|
|
def init():
|
|
global TOKENS
|
|
TOKENS = [
|
|
'CSTART',
|
|
'CMID',
|
|
'CEND',
|
|
'RPAREN',
|
|
'LPAREN',
|
|
'ENDLINE',
|
|
'RETVAL',
|
|
'PREPROCESS',
|
|
'ID',
|
|
'PLUS',
|
|
'MINUS',
|
|
'DIV',
|
|
'MULT',
|
|
'ASSIGN',
|
|
'EQUAL',
|
|
'LBRACE',
|
|
'RBRACE',
|
|
'COMMA',
|
|
'SEMICOLON',
|
|
'LANGLE',
|
|
'RANGLE',
|
|
'POINTER'
|
|
]
|
|
|
|
RESERVED = {
|
|
'auto' : 'AUTO',
|
|
'break' : 'BREAK',
|
|
'case' : 'CASE',
|
|
'char' : 'CHAR',
|
|
'const' : 'CONST',
|
|
'continue' : 'CONTINUE',
|
|
'default' : 'DEFAULT',
|
|
'do' : 'DO',
|
|
'int' : 'INT',
|
|
'long' : 'LONG',
|
|
'register' : 'REGISTER',
|
|
'return' : 'RETURN',
|
|
'short' : 'SHORT',
|
|
'signed' : 'SIGNED',
|
|
'sizeof' : 'SIZEOF',
|
|
'static' : 'STATIC',
|
|
'struct' : 'STRUCT',
|
|
'switch' : 'SWITCH',
|
|
'typedef' : 'TYPEDEF',
|
|
'union' : 'UNION',
|
|
'unsigned' : 'UNSIGNED',
|
|
'void' : 'VOID',
|
|
'volatile' : 'VOLATILE',
|
|
'while' : 'WHILE',
|
|
'double' : 'DOUBLE',
|
|
'else' : 'ELSE',
|
|
'enum' : 'ENUM',
|
|
'extern' : 'EXTERN',
|
|
'float' : 'FLOAT',
|
|
'for' : 'FOR',
|
|
'goto' : 'GOTO',
|
|
'if' : 'IF'
|
|
}
|
|
|
|
global PREPROCESSOR_DIRECTIVES
|
|
PREPROCESSOR_DIRECTIVES = {
|
|
'#include' : 'PREPROCESS',
|
|
'#define' : 'PREPROCESS',
|
|
'#undef' : 'PREPROCESS',
|
|
'#if' : 'PREPROCESS',
|
|
'#ifdef' : 'PREPROCESS',
|
|
'#ifndef' : 'PREPROCESS',
|
|
'#error' : 'PREPROCESS',
|
|
'__FILE__' : 'PREPROCESS',
|
|
'__LINE__' : 'PREPROCESS',
|
|
'__DATE__' : 'PREPROCESS',
|
|
'__TIME__' : 'PREPROCESS',
|
|
'__TIMESTAMP__' : 'PREPROCESS',
|
|
'pragma' : 'PREPROCESS',
|
|
'#' : 'PREPROCESS',
|
|
'##' : 'PREPROCESS'
|
|
}
|
|
|
|
global REGULAR
|
|
REGULAR = {
|
|
'/*' : 'CSTART',
|
|
'*/' : 'CEND',
|
|
'=' : 'ASSIGN',
|
|
'==' : 'EQUAL',
|
|
'{' : 'LBRACE',
|
|
'}' : 'RBRACE',
|
|
'\+' : 'PLUS',
|
|
'-' : 'MINUS',
|
|
'\*' : 'MULT',
|
|
'/' : 'DIV',
|
|
'\(' : 'LPAREN',
|
|
'\)' : 'RPAREN',
|
|
',' : 'COMMA',
|
|
';' : 'SEMICOLON',
|
|
'\<' : 'LANGLE',
|
|
'\>' : 'RANGLE'
|
|
}
|
|
|
|
global NASM_PREPROCESS_DIRECTIVES
|
|
NASM_PREPROCESS_DIRECTIVES = {
|
|
'#include' : '$include',
|
|
'#define' : '$define',
|
|
'#undef' : '$undef',
|
|
'#if' : '$if',
|
|
'#ifdef' : '$ifdef',
|
|
'#ifndef' : '$ifndef',
|
|
'#error' : '$error',
|
|
'__FILE__' : '__FILE__',
|
|
'__LINE__' : '__LINE__',
|
|
'__DATE__' : '__DATE__',
|
|
'__TIME__' : '__TIME__',
|
|
'__TIMESTAMP__' : '__TIMESTAMP__',
|
|
'pragma' : 'pragma',
|
|
'#' : '#',
|
|
'##' : '##'
|
|
}
|
|
|
|
TOKENS += RESERVED.values()
|