X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=kll.py;h=37cfd5fc285c8d354491726e983c6b96d2f27cac;hb=0cb66411aad82f7bf9fa20e4942be9bacefcfa86;hp=6d79b2e0295e75f077c0e57b401394a8c912bf80;hpb=b4e4a13811f38b2ba6b36b0fd194c4dd399f61f8;p=kiibohd-kll.git diff --git a/kll.py b/kll.py index 6d79b2e..37cfd5f 100755 --- a/kll.py +++ b/kll.py @@ -2,7 +2,7 @@ # KLL Compiler # Keyboard Layout Langauge # -# Copyright (C) 2014 by Jacob Alexander +# Copyright (C) 2014-2015 by Jacob Alexander # # This file is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -93,9 +93,15 @@ def processCommandLineArgs(): pArgs.add_argument( '-t', '--template', type=str, default="templates/kiibohdKeymap.h", help="Specify template used to generate the keymap.\n" "Default: templates/kiibohdKeymap.h" ) - pArgs.add_argument( '-o', '--output', type=str, default="templateKeymap.h", + pArgs.add_argument( '--defines-template', type=str, default="templates/kiibohdDefs.h", + help="Specify template used to generate kll_defs.h.\n" + "Default: templates/kiibohdDefs.h" ) + pArgs.add_argument( '-o', '--output', type=str, default="generatedKeymap.h", help="Specify output file. Writes to current working directory by default.\n" "Default: generatedKeymap.h" ) + pArgs.add_argument( '--defines-output', type=str, default="kll_defs.h", + help="Specify output path for kll_defs.h. Writes to current working directory by default.\n" + "Default: kll_defs.h" ) pArgs.add_argument( '-h', '--help', action="help", help="This message." ) @@ -122,7 +128,7 @@ def processCommandLineArgs(): for filename in partial: checkFileExists( filename ) - return (baseFiles, defaultFiles, partialFileSets, args.backend, args.template, args.output) + return (baseFiles, defaultFiles, partialFileSets, args.backend, args.template, args.defines_template, args.output, args.defines_output) @@ -165,7 +171,7 @@ def tokenize( string ): ## Map Arrays macros_map = Macros() -variable_dict = dict() +variables_dict = Variables() capabilities_dict = Capabilities() @@ -255,6 +261,9 @@ def make_seqString( token ): def make_string( token ): return token[1:-1] +def make_unseqString( token ): + return token[1:-1] + def make_number( token ): return int( token, 0 ) @@ -378,7 +387,6 @@ def optionExpansion( sequences ): curLeafList[ leaf ] = 0 if leaf + 1 < len( curLeafList ): curLeafList[ leaf + 1 ] += 1 - break return expandedSequences @@ -441,31 +449,36 @@ def eval_variable( name, content ): for item in content: assigned_content += str( item ) - variable_dict[ name ] = assigned_content + variables_dict.assignVariable( name, assigned_content ) def eval_capability( name, function, args ): capabilities_dict[ name ] = [ function, args ] +def eval_define( name, cdefine_name ): + variables_dict.defines[ name ] = cdefine_name + map_scanCode = unarg( eval_scanCode ) map_usbCode = unarg( eval_usbCode ) set_variable = unarg( eval_variable ) set_capability = unarg( eval_capability ) +set_define = unarg( eval_define ) ## Sub Rules -usbCode = tokenType('USBCode') >> make_usbCode -scanCode = tokenType('ScanCode') >> make_scanCode -name = tokenType('Name') -number = tokenType('Number') >> make_number -comma = tokenType('Comma') -dash = tokenType('Dash') -plus = tokenType('Plus') -content = tokenType('VariableContents') -string = tokenType('String') >> make_string -unString = tokenType('String') # When the double quotes are still needed for internal processing -seqString = tokenType('SequenceString') >> make_seqString +usbCode = tokenType('USBCode') >> make_usbCode +scanCode = tokenType('ScanCode') >> make_scanCode +name = tokenType('Name') +number = tokenType('Number') >> make_number +comma = tokenType('Comma') +dash = tokenType('Dash') +plus = tokenType('Plus') +content = tokenType('VariableContents') +string = tokenType('String') >> make_string +unString = tokenType('String') # When the double quotes are still needed for internal processing +seqString = tokenType('SequenceString') >> make_seqString +unseqString = tokenType('SequenceString') >> make_unseqString # For use with variables # Code variants code_end = tokenType('CodeEnd') @@ -506,13 +519,16 @@ resultCode_outerList = capFunc_sequence >> optionExpansion >> usbCodeToCapab ## Main Rules #| = ; -variable_contents = name | content | string | number | comma | dash +variable_contents = name | content | string | number | comma | dash | unseqString variable_expression = name + skip( operator('=') ) + oneplus( variable_contents ) + skip( eol ) >> set_variable #| => ; capability_arguments = name + skip( operator(':') ) + number + skip( maybe( comma ) ) capability_expression = name + skip( operator('=>') ) + name + skip( parenthesis('(') ) + many( capability_arguments ) + skip( parenthesis(')') ) + skip( eol ) >> set_capability +#| => ; +define_expression = name + skip( operator('=>') ) + name + skip( eol ) >> set_define + #| : ; operatorTriggerResult = operator(':') | operator(':+') | operator(':-') scanCode_expression = triggerCode_outerList + operatorTriggerResult + resultCode_outerList + skip( eol ) >> map_scanCode @@ -522,7 +538,7 @@ def parse( tokenSequence ): """Sequence(Token) -> object""" # Top-level Parser - expression = scanCode_expression | usbCode_expression | variable_expression | capability_expression + expression = scanCode_expression | usbCode_expression | variable_expression | capability_expression | define_expression kll_text = many( expression ) kll_file = maybe( kll_text ) + skip( finished ) @@ -536,27 +552,62 @@ def processKLLFile( filename ): data = file.read() tokenSequence = tokenize( data ) #print ( pformat( tokenSequence ) ) # Display tokenization - tree = parse( tokenSequence ) + try: + tree = parse( tokenSequence ) + except NoParseError as e: + print("Error parsing %s. %s" % (filename, e.msg), file=sys.stderr) + sys.exit(1) + + +### Misc Utility Functions ### + +def gitRevision( kllPath ): + import subprocess + # Change the path to where kll.py is + origPath = os.getcwd() + os.chdir( kllPath ) + + # Just in case git can't be found + try: + # Get hash of the latest git commit + revision = subprocess.check_output( ['git', 'rev-parse', 'HEAD'] ).decode()[:-1] + + # Get list of files that have changed since the commit + changed = subprocess.check_output( ['git', 'diff-index', '--name-only', 'HEAD', '--'] ).decode().splitlines() + except: + revision = "" + changed = [] + + # Change back to the old working directory + os.chdir( origPath ) + + return revision, changed ### Main Entry Point ### if __name__ == '__main__': - (baseFiles, defaultFiles, partialFileSets, backend_name, template, output) = processCommandLineArgs() + (baseFiles, defaultFiles, partialFileSets, backend_name, template, defines_template, output, defines_output) = processCommandLineArgs() + + # Look up git information on the compiler + gitRev, gitChanges = gitRevision( os.path.dirname( os.path.realpath( __file__ ) ) ) # Load backend module global backend backend_import = importlib.import_module( "backends.{0}".format( backend_name ) ) - backend = backend_import.Backend( template ) + backend = backend_import.Backend( template, defines_template ) # Process base layout files for filename in baseFiles: + variables_dict.setCurrentFile( filename ) processKLLFile( filename ) macros_map.completeBaseLayout() # Indicates to macros_map that the base layout is complete + variables_dict.baseLayoutFinished() # Default combined layer for filename in defaultFiles: + variables_dict.setCurrentFile( filename ) processKLLFile( filename ) # Apply assignment cache, see 5.1.2 USB Codes for why this is necessary macros_map.replayCachedAssignments() @@ -565,9 +616,13 @@ if __name__ == '__main__': for partial in partialFileSets: # Increment layer for each -p option macros_map.addLayer() + variables_dict.incrementLayer() # DefaultLayer is layer 0 + # Iterate and process each of the file in the layer for filename in partial: + variables_dict.setCurrentFile( filename ) processKLLFile( filename ) + # Apply assignment cache, see 5.1.2 USB Codes for why this is necessary macros_map.replayCachedAssignments() # Remove un-marked keys to complete the partial layer @@ -577,10 +632,16 @@ if __name__ == '__main__': macros_map.generate() # Process needed templating variables using backend - backend.process( capabilities_dict, macros_map ) + backend.process( + capabilities_dict, + macros_map, + variables_dict, + gitRev, + gitChanges + ) # Generate output file using template and backend - backend.generate( output ) + backend.generate( output, defines_output ) # Successful Execution sys.exit( 0 )