def main(): # Parse command line arguments. options = parse_args(constants.TOOL_PARSER) log.set_verbosity(options.verbosity) # Parse each source. ast = [] if options.file: for f in options.file: s = f.read() try: ast += parse_to_ast(s, options.cpp, options.cpp_flag) if options.resolve_imports: ast, _ = resolve_imports(ast, \ os.path.dirname(f.name), options.import_path, options.cpp, options.cpp_flag) except CAmkESSyntaxError as e: e.set_column(s) log.error('%s:%s' % (f.name, str(e))) return -1 except Exception: log.exception('Error during lexing/parsing \'%s\''% f.name) return -1 finally: f.close() else: s = sys.stdin.read() try: ast += parse_to_ast(s, options.cpp, options.cpp_flag) if options.resolve_imports: ast, _ = resolve_imports(ast, \ os.curdir, options.import_path, options.cpp, options.cpp_flag) except Exception: log.exception('Error during lexing/parsing') return -1 ast = dedupe(ast) if options.resolve_references: ast = resolve_references(ast) # Generate the output and print this. out = show(ast) print pretty(out) return 0
def main(): # Input text. s = 'assembly { composition { component foo bar; } }' print 'Input:\n%s\n' % s # 2. Translate your input into an AST. ast = camkes.parse_to_ast(s) # At this point, ast contains a list of objects whose types are defined in # GenericObjects.py, IDLObjects.py and ADLObjects.py. If you want to # manipulate the AST you will want to import these. # 3. If your input contains import statements that refer to other files, # you can use resolve_imports to inline and parse these into your ast. ast, _ = camkes.resolve_imports(ast, curdir) # 4. If your input contains any references these will be present in the AST # as objects of type GenericObjects.Reference. For example, in the input in # this example the component type 'foo' is a reference to a component # definition that is expected to be provided elsewhere. After performing # reference resolution there may still be references in the AST. This # occurs when your references cannot be resolved. For example, in the input # here 'foo' is never actually defined. ast = camkes.resolve_references(ast, False) # 5. If you want to get the AST in an output format call show(). This # accepts the AST itself. print 'Output:\n%s\n' % camkes.show(ast) # 6. Some output printers implement a pretty printing function to format the # output in a human-readable way. Access this with pretty(). print 'Pretty printed:\n%s\n' % camkes.pretty(camkes.show(ast)) return 0
def main(): # Input text. s = 'assembly { composition { component foo bar; } }' sys.stdout.write('Input:\n%s\n\n' % s) # 2. Translate your input into an AST. ast = camkes.parse_to_ast(s) # At this point, ast contains a list of objects whose types are defined in # Objects.py. If you want to manipulate the AST you will want to import the # AST module. # 3. If your input contains import statements that refer to other files, # you can use resolve_imports to inline and parse these into your ast. ast, _ = camkes.resolve_imports(ast, curdir) # 4. If your input contains any references these will be present in the AST # as objects of type camkes.ast.Reference. For example, in the input in # this example the component type 'foo' is a reference to a component # definition that is expected to be provided elsewhere. After performing # reference resolution there may still be references in the AST. This # occurs when your references cannot be resolved. For example, in the input # here 'foo' is never actually defined. ast = camkes.resolve_references(ast, False) # 5. If you want to get the AST in an output format call show(). This # accepts the AST itself. sys.stdout.write('Output:\n%s\n\n' % camkes.show(ast)) # 6. Some output printers implement a pretty printing function to format the # output in a human-readable way. Access this with pretty(). sys.stdout.write('Pretty printed:\n%s\n\n' % camkes.pretty(camkes.show(ast))) return 0
def main(argv): # Parse input vm_mode = False try: opts, args = getopt.getopt(argv, "cmv:") except getopt.GetoptError as err: print str(err) sys.exit(1) if len(args) == 0: print "Not enough arguments" sys.exit(1) elif len(args) > 1: print "Too many args" sys.exit(1) else: project_camkes = args[0] if not os.path.isfile(APPS_FOLDER + project_camkes): print "File not found: %s" % APPS_FOLDER + project_camkes sys.exit(1) for opt, arg in opts: if opt == "-c": clean_debug(project_camkes) sys.exit(0) if opt == "-v": vm_mode = True vm = arg # Open camkes file for parsing with open(APPS_FOLDER + project_camkes) as camkes_file: lines = camkes_file.readlines() # Save any imports and add them back in imports = "" import_regex = re.compile(r'import .*') for line in lines: if import_regex.match(line): imports += line camkes_text = "\n".join(lines) # Parse using camkes parser camkes_builtin_path = os.path.realpath(__file__ + '/../../camkes/include/builtin') include_path = [camkes_builtin_path] if vm_mode: print "vm mode" cpp = True config_path = os.path.realpath(__file__ + '/../../../apps/%s/configurations' % vm) vm_components_path = os.path.realpath(__file__ + '/../../../apps/%s/../../components/VM' % vm) plat_includes = os.path.realpath(__file__ + '/../../../kernel/include/plat/%s' % PLAT) cpp_options = ['-DCAMKES_VM_CONFIG=%s' % vm, "-I"+config_path, "-I"+vm_components_path, "-I"+plat_includes] include_path.append(os.path.realpath(__file__ + "/../../../projects/vm/components")) include_path.append(os.path.realpath(__file__ + "/../../../projects/vm/interfaces")) else: cpp = False cpp_options = [] target_ast = parser.parse_to_ast(camkes_text, cpp, cpp_options) # Resolve other imports project_dir = os.path.dirname(os.path.realpath(APPS_FOLDER + project_camkes)) + "/" target_ast, _ = parser.resolve_imports(target_ast, project_dir, include_path, cpp, cpp_options) target_ast = parser.resolve_references(target_ast) # Find debug components declared in the camkes file debug_components = get_debug_components(target_ast) # Use the declared debug components to find the types we must generate debug_types, target_assembly = get_debug_component_types(target_ast, debug_components) # Generate the new types target_ast = create_debug_component_types(target_ast, debug_types) # Add declarations for the new types target_ast = add_debug_declarations(target_ast, debug_components, target_assembly) # Get the static definitions needed every time debug_definitions = get_debug_definitions() # Generate server based on debug components debug_definitions += generate_server_component(debug_components) # Update makefile with the new debug camkes update_makefile(project_camkes, debug_types) # Copy the templates into the project directory copy_templates(project_camkes) # Add our debug definitions new_camkes = parser.pretty(parser.show(target_ast)) # Reparse and rearrange the included code procedures = [] main_ast = [] new_ast = parser.parse_to_ast(new_camkes, cpp, cpp_options) for component in new_ast: if isinstance(component, ast.Objects.Procedure): procedures.append(component) else: main_ast.append(component) final_camkes = imports + debug_definitions + parser.pretty(parser.show(procedures + main_ast)) # Write new camkes file with open(APPS_FOLDER + project_camkes + ".dbg", 'w') as f: for line in final_camkes: f.write(line) # Write a gdbinit file name_regex = re.compile(r"(.*)/") search = name_regex.search(project_camkes) if debug_components: write_gdbinit(search.group(1), debug_components) else: print "No debug components found"