def peer(lexer: Lexer, type_: str): peer = { "name": {}, "type": type_, "properties": {}, "requests": {}, "signals": {}, } lexer.eat_whitespace() peer["name"] = identifier(lexer) lexer.eat_whitespace() if not lexer.current_is("("): return peer lexer.skip_and_expect("(") lexer.eat_whitespace() while not lexer.current_is(")"): peer_field(lexer, peer) lexer.eat_whitespace() lexer.skip_and_expect(";") lexer.eat_whitespace() lexer.skip_and_expect(")") lexer.eat_whitespace() return peer
def protocol(lexer: Lexer): protocol = { "properties": {}, "enumerations": {}, "structures": {}, } lexer.eat_whitespace() lexer.skip_and_expect("protocol") lexer.eat_whitespace() lexer.skip_and_expect("(") lexer.eat_whitespace() while not lexer.current_is(")"): protocol_field(lexer, protocol) lexer.eat_whitespace() lexer.skip_and_expect(";") lexer.eat_whitespace() lexer.eat_whitespace() lexer.skip_and_expect(")") lexer.eat_whitespace() lexer.skip_and_expect(";") lexer.eat_whitespace() return protocol
def identifier(lexer: Lexer) -> str: tmp = "" while lexer.current_is( "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_1234567890"): tmp = tmp + lexer.current() lexer.forward() return tmp
def string(lexer: Lexer) -> str: lexer.skip_and_expect("\"") tmp = "" while not lexer.current_is("\""): tmp = tmp + lexer.current() lexer.forward() lexer.forward() return tmp
def enumeration(lexer: Lexer): lexer.eat_whitespace() enumeration_name = identifier(lexer) enumeration_values = [] lexer.eat_whitespace() lexer.skip_and_expect("(") lexer.eat_whitespace() while not lexer.current_is(")"): enumeration_values.append(identifier(lexer)) lexer.eat_whitespace() lexer.skip_and_expect(",") lexer.eat_whitespace() lexer.skip_and_expect(")") return {enumeration_name: enumeration_values}
def struct_body(lexer: Lexer): body = {} lexer.eat_whitespace() lexer.skip_and_expect("(") lexer.eat_whitespace() while not lexer.current_is(")"): field_type = identifier(lexer) lexer.eat_whitespace() field_name = identifier(lexer) lexer.eat_whitespace() lexer.skip_word(",") lexer.eat_whitespace() body.update({field_name: field_type}) lexer.skip_and_expect(")") return body