def test_many_class_vars():
    xml_text = """
  <tokens>
    <keyword> class </keyword>
    <identifier> Main </identifier>
    <symbol> { </symbol>
    <keyword> static </keyword>
    <keyword> boolean </keyword>
    <identifier> test </identifier>
    <symbol> ; </symbol>
    <keyword> field </keyword>
    <keyword> int </keyword>
    <identifier> direction </identifier>
    <symbol> ; </symbol>
    <symbol> } </symbol>
  </tokens>
  """

    pp = Parser(xml_text, is_input_str=True)
    pp.CompileClass()

    tree = ET.ElementTree(pp.root)
    lib.indent(pp.root)

    count = len(pp.root)
    print(count)

    # fn = 'test.xml'
    # tree.write(fn)

    assert (count == 6)
    print('Test class vars passed!')
예제 #2
0
def tokenize_file(input, output=None):
    root = ET.Element("tokens")

    tk = JackTokenizer.Tokenizer(input)
    while (tk.hasMoreTokens()):
        tk.advance()

        tk_type = tk.tokenType()
        element = None
        if tk_type == JackTokenizer.TYPE_KEYWORD:
            kw = tk.keyword()
            ET.SubElement(root, 'keyword').text = pretty_token(kw)

        elif tk_type == JackTokenizer.TYPE_SYMBOL:
            kw = tk.symbol()
            ET.SubElement(root, 'symbol').text = pretty_token(kw)

        elif tk_type == JackTokenizer.TYPE_IDENTIFIER:
            kw = tk.identifier()

            #TODO: output symbol table metadata.

            ET.SubElement(root, 'identifier').text = pretty_token(kw)

        elif tk_type == JackTokenizer.TYPE_INT_CONST:
            kw = tk.intVal()
            ET.SubElement(root, 'integerConstant').text = pretty_token(kw)

        elif tk_type == JackTokenizer.TYPE_STRING_CONT:
            kw = tk.stringVal()
            ET.SubElement(root, 'stringConstant').text = pretty_token(kw)

    tree = ET.ElementTree(root)
    lib.indent(root)

    if output:
        fn = output
    else:
        fn = '%sT.xml' % input

    tree.write(fn)
    def WriteAsFile(self):
        tree = ET.ElementTree(self.root)
        lib.indent(self.root)

        tree.write(file_or_filename=self.output, short_empty_elements=False)