Ejemplo n.º 1
0
    def include(self, filename: str) -> str:
        """ Changes FILENAME and line count
        """
        if filename != STDIN and filename in set(
                x.filename for x in self.filestack):  # Already included?
            self.warning('Recursive inclusion')

        self.filestack.append(
            LexerState(filename, 1, self.lex, self.input_data))

        if self.lex is None:
            self.lex = lex.lex(object=self)
        else:
            self.lex = self.lex.clone()
            self.lex.lineno = 1  # resets line number

        result = self.put_current_line()  # First #line start with \n (EOL)

        try:
            if filename == STDIN:
                self.input_data = sys.stdin.read()
            else:
                self.input_data = src.api.utils.read_txt_file(filename)
            if len(self.input_data) and self.input_data[-1] != EOL:
                self.input_data += EOL
        except IOError:
            self.input_data = EOL

        self.lex.input(self.input_data)
        return result
Ejemplo n.º 2
0
            self, t):
        """ error handling rule. Should never happen!
        """
        pass  # The lexer will raise an exception here. This is intended

    def t_asm_asmcomment_if_ANY(self, t):
        r'.'
        self.error("illegal preprocessor character '%s'" % t.value[0])

    def t_asm_asmcomment_if_msg_error(self, t):
        """ error handling rule. Should never happen!
        """
        pass


# --------------------- PREPROCESSOR FUNCTIONS -------------------

# Needed for states
lex.lex(object=Lexer())

# ------------------ Test if called from cmd line ---------------
if __name__ == '__main__':  # For testing purposes
    lexer = Lexer()
    lexer.include(sys.argv[1])

    while 1:
        tok = lexer.token()
        if not tok:
            break
        print(tok)
Ejemplo n.º 3
0
 def set_state(self, new_input: str, new_lexpos: int = 0):
     self.lex = lex.lex(object=self)
     self.lex.input(new_input)
     self.lexpos = new_lexpos
Ejemplo n.º 4
0
 def input(self, str):
     """ Defines input string, removing current lexer.
     """
     self.input_data = str
     self.lex = lex.lex(object=self)
     self.lex.input(self.input_data)
Ejemplo n.º 5
0
    def token(self):
        return self.lex.token()

    def find_column(self, token):
        """ Compute column:
                - token is a token instance
        """
        i = token.lexpos
        while i > 0:
            if self.input_data[i - 1] == '\n':
                break
            i -= 1

        column = token.lexpos - i + 1

        return column


# --------------------- PREPROCESSOR FUNCTIONS -------------------

# Needed for states
tmp = lex.lex(object=Lexer())

if __name__ == '__main__':
    tmp.input(open(sys.argv[1]).read())
    tok = tmp.token()
    while tok:
        print(tok)
        tok = tmp.token()
Ejemplo n.º 6
0
    c = i = token.lexpos
    input = token.lexer.lexdata
    c -= 1
    while c > 0 and input[c] in (' ', '\t'):
        c -= 1

    while i > 0:
        if input[i] == '\n':
            break
        i -= 1

    column = c - i

    if column == 0:
        column += 1

    return column == 1


lexer = lex.lex()

if __name__ == '__main__':  # For testing purposes
    lexer.input(open(sys.argv[1], 'rt').read())

    while 1:
        tok = lexer.token()
        if not tok:
            break
        print(tok)