X-Git-Url: https://git.donarmstrong.com/?a=blobdiff_plain;f=kll.py;h=37cfd5fc285c8d354491726e983c6b96d2f27cac;hb=0cb66411aad82f7bf9fa20e4942be9bacefcfa86;hp=1e477a1efb0103ad65f45923ebdd38f8f609ab6a;hpb=1f0bf65417613b1cb3a5bfd744750e1e823c8622;p=kiibohd-kll.git diff --git a/kll.py b/kll.py index 1e477a1..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 @@ -61,12 +61,17 @@ argparse._ = textFormatter_gettext ### Argument Parsing ### +def checkFileExists( filename ): + if not os.path.isfile( filename ): + print ( "{0} {1} does not exist...".format( ERROR, filename ) ) + sys.exit( 1 ) + def processCommandLineArgs(): # Setup argument processor pArgs = argparse.ArgumentParser( usage="%(prog)s [options] ...", description="Generates .h file state tables and pointer indices from KLL .kll files.", - epilog="Example: {0} TODO".format( os.path.basename( sys.argv[0] ) ), + epilog="Example: {0} mykeyboard.kll -d colemak.kll -p hhkbpro2.kll -p symbols.kll".format( os.path.basename( sys.argv[0] ) ), formatter_class=argparse.RawTextHelpFormatter, add_help=False, ) @@ -79,16 +84,24 @@ def processCommandLineArgs(): pArgs.add_argument( '-b', '--backend', type=str, default="kiibohd", help="Specify target backend for the KLL compiler.\n" "Default: kiibohd" ) + pArgs.add_argument( '-d', '--default', type=str, nargs='+', + help="Specify .kll files to layer on top of the default map to create a combined map." ) pArgs.add_argument( '-p', '--partial', type=str, nargs='+', action='append', help="Specify .kll files to generate partial map, multiple files per flag.\n" "Each -p defines another partial map.\n" "Base .kll files (that define the scan code maps) must be defined for each partial map." ) - pArgs.add_argument( '-t', '--template', type=str, default="templateKeymap.h", + pArgs.add_argument( '-t', '--template', type=str, default="templates/kiibohdKeymap.h", help="Specify template used to generate the keymap.\n" - "Default: templateKeymap.h" ) - pArgs.add_argument( '-o', '--output', type=str, default="templateKeymap.h", + "Default: templates/kiibohdKeymap.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." ) @@ -96,24 +109,26 @@ def processCommandLineArgs(): args = pArgs.parse_args() # Parameters - defaultFiles = args.files + baseFiles = args.files + defaultFiles = args.default partialFileSets = args.partial + if defaultFiles is None: + defaultFiles = [] if partialFileSets is None: partialFileSets = [[]] # Check file existance + for filename in baseFiles: + checkFileExists( filename ) + for filename in defaultFiles: - if not os.path.isfile( filename ): - print ( "{0} {1} does not exist...".format( ERROR, filename ) ) - sys.exit( 1 ) + checkFileExists( filename ) for partial in partialFileSets: for filename in partial: - if not os.path.isfile( filename ): - print ( "{0} {1} does not exist...".format( ERROR, filename ) ) - sys.exit( 1 ) + checkFileExists( filename ) - return (defaultFiles, partialFileSets, args.backend, args.template, args.output) + return (baseFiles, defaultFiles, partialFileSets, args.backend, args.template, args.defines_template, args.output, args.defines_output) @@ -133,10 +148,10 @@ def tokenize( string ): ( 'CodeEnd', ( r'\]', ) ), ( 'String', ( r'"[^"]*"', VERBOSE ) ), ( 'SequenceString', ( r"'[^']*'", ) ), + ( 'Operator', ( r'=>|:\+|:-|:|=', ) ), ( 'Comma', ( r',', ) ), ( 'Dash', ( r'-', ) ), ( 'Plus', ( r'\+', ) ), - ( 'Operator', ( r'=>|:|=', ) ), ( 'Parenthesis', ( r'\(|\)', ) ), ( 'Number', ( r'-?(0x[0-9a-fA-F]+)|(0|([1-9][0-9]*))', VERBOSE ) ), ( 'Name', ( r'[A-Za-z_][A-Za-z_0-9]*', ) ), @@ -155,9 +170,8 @@ def tokenize( string ): ### Parsing ### ## Map Arrays -scanCode_map = [ None ] * 0xFF # Define 8 bit address width -usbCode_map = [ None ] * 0xFF # Define 8 bit address width -variable_dict = dict() +macros_map = Macros() +variables_dict = Variables() capabilities_dict = Capabilities() @@ -247,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 ) @@ -301,6 +318,9 @@ eol = a( Token( 'EndOfLine', ';' ) ) def listElem( item ): return [ item ] +def listToTuple( items ): + return tuple( items ) + # Flatten only the top layer (list of lists of ...) def oneLayerFlatten( items ): mainList = [] @@ -310,6 +330,21 @@ def oneLayerFlatten( items ): return mainList + # Capability arguments may need to be expanded (e.g. 1 16 bit argument needs to be 2 8 bit arguments for the state machine) +def capArgExpander( items ): + newArgs = [] + # For each defined argument in the capability definition + for arg in range( 0, len( capabilities_dict[ items[0] ][1] ) ): + argLen = capabilities_dict[ items[0] ][1][ arg ][1] + num = items[1][ arg ] + byteForm = num.to_bytes( argLen, byteorder='little' ) # XXX Yes, little endian from how the uC structs work + + # For each sub-argument, split into byte-sized chunks + for byte in range( 0, argLen ): + newArgs.append( byteForm[ byte ] ) + + return tuple( [ items[0], tuple( newArgs ) ] ) + # Expand ranges of values in the 3rd dimension of the list, to a list of 2nd lists # i.e. [ sequence, [ combo, [ range ] ] ] --> [ [ sequence, [ combo ] ],