]> git.donarmstrong.com Git - kiibohd-kll.git/blob - kll.py
Adding more generation debug output
[kiibohd-kll.git] / kll.py
1 #!/usr/bin/env python3
2 # KLL Compiler
3 # Keyboard Layout Langauge
4 #
5 # Copyright (C) 2014-2015 by Jacob Alexander
6 #
7 # This file is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # This file is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with this file.  If not, see <http://www.gnu.org/licenses/>.
19
20 ### Imports ###
21
22 import argparse
23 import io
24 import os
25 import re
26 import sys
27 import token
28 import importlib
29
30 from tokenize import generate_tokens
31 from re       import VERBOSE
32 from pprint   import pformat
33
34 from kll_lib.hid_dict   import *
35 from kll_lib.containers import *
36
37 from funcparserlib.lexer  import make_tokenizer, Token, LexerError
38 from funcparserlib.parser import (some, a, many, oneplus, skip, finished, maybe, skip, forward_decl, NoParseError)
39
40
41
42 ### Decorators ###
43
44  ## Print Decorator Variables
45 ERROR = '\033[5;1;31mERROR\033[0m:'
46
47
48  ## Python Text Formatting Fixer...
49  ##  Because the creators of Python are averse to proper capitalization.
50 textFormatter_lookup = {
51         "usage: "            : "Usage: ",
52         "optional arguments" : "Optional Arguments",
53 }
54
55 def textFormatter_gettext( s ):
56         return textFormatter_lookup.get( s, s )
57
58 argparse._ = textFormatter_gettext
59
60
61
62 ### Argument Parsing ###
63
64 def checkFileExists( filename ):
65         if not os.path.isfile( filename ):
66                 print ( "{0} {1} does not exist...".format( ERROR, filename ) )
67                 sys.exit( 1 )
68
69 def processCommandLineArgs():
70         # Setup argument processor
71         pArgs = argparse.ArgumentParser(
72                 usage="%(prog)s [options] <file1>...",
73                 description="Generates .h file state tables and pointer indices from KLL .kll files.",
74                 epilog="Example: {0} mykeyboard.kll -d colemak.kll -p hhkbpro2.kll -p symbols.kll".format( os.path.basename( sys.argv[0] ) ),
75                 formatter_class=argparse.RawTextHelpFormatter,
76                 add_help=False,
77 )
78
79         # Positional Arguments
80         pArgs.add_argument( 'files', type=str, nargs='+',
81                 help=argparse.SUPPRESS ) # Suppressed help output, because Python output is verbosely ugly
82
83         # Optional Arguments
84         pArgs.add_argument( '-b', '--backend', type=str, default="kiibohd",
85                 help="Specify target backend for the KLL compiler.\n"
86                 "Default: kiibohd" )
87         pArgs.add_argument( '-d', '--default', type=str, nargs='+',
88                 help="Specify .kll files to layer on top of the default map to create a combined map." )
89         pArgs.add_argument( '-p', '--partial', type=str, nargs='+', action='append',
90                 help="Specify .kll files to generate partial map, multiple files per flag.\n"
91                 "Each -p defines another partial map.\n"
92                 "Base .kll files (that define the scan code maps) must be defined for each partial map." )
93         pArgs.add_argument( '-t', '--template', type=str, default="templates/kiibohdKeymap.h",
94                 help="Specify template used to generate the keymap.\n"
95                 "Default: templates/kiibohdKeymap.h" )
96         pArgs.add_argument( '--defines-template', type=str, default="templates/kiibohdDefs.h",
97                 help="Specify template used to generate kll_defs.h.\n"
98                 "Default: templates/kiibohdDefs.h" )
99         pArgs.add_argument( '-o', '--output', type=str, default="generatedKeymap.h",
100                 help="Specify output file. Writes to current working directory by default.\n"
101                 "Default: generatedKeymap.h" )
102         pArgs.add_argument( '--defines-output', type=str, default="kll_defs.h",
103                 help="Specify output path for kll_defs.h. Writes to current working directory by default.\n"
104                 "Default: kll_defs.h" )
105         pArgs.add_argument( '-h', '--help', action="help",
106                 help="This message." )
107
108         # Process Arguments
109         args = pArgs.parse_args()
110
111         # Parameters
112         baseFiles = args.files
113         defaultFiles = args.default
114         partialFileSets = args.partial
115         if defaultFiles is None:
116                 defaultFiles = []
117         if partialFileSets is None:
118                 partialFileSets = [[]]
119
120         # Check file existance
121         for filename in baseFiles:
122                 checkFileExists( filename )
123
124         for filename in defaultFiles:
125                 checkFileExists( filename )
126
127         for partial in partialFileSets:
128                 for filename in partial:
129                         checkFileExists( filename )
130
131         return (baseFiles, defaultFiles, partialFileSets, args.backend, args.template, args.defines_template, args.output, args.defines_output)
132
133
134
135 ### Tokenizer ###
136
137 def tokenize( string ):
138         """str -> Sequence(Token)"""
139
140         # Basic Tokens Spec
141         specs = [
142                 ( 'Comment',          ( r' *#.*', ) ),
143                 ( 'Space',            ( r'[ \t\r\n]+', ) ),
144                 ( 'USBCode',          ( r'U(("[^"]+")|(0x[0-9a-fA-F]+)|([0-9]+))', ) ),
145                 ( 'USBCodeStart',     ( r'U\[', ) ),
146                 ( 'ScanCode',         ( r'S((0x[0-9a-fA-F]+)|([0-9]+))', ) ),
147                 ( 'ScanCodeStart',    ( r'S\[', ) ),
148                 ( 'CodeEnd',          ( r'\]', ) ),
149                 ( 'String',           ( r'"[^"]*"', VERBOSE ) ),
150                 ( 'SequenceString',   ( r"'[^']*'", ) ),
151                 ( 'Operator',         ( r'=>|:\+|:-|:|=', ) ),
152                 ( 'Comma',            ( r',', ) ),
153                 ( 'Dash',             ( r'-', ) ),
154                 ( 'Plus',             ( r'\+', ) ),
155                 ( 'Parenthesis',      ( r'\(|\)', ) ),
156                 ( 'Number',           ( r'-?(0x[0-9a-fA-F]+)|(0|([1-9][0-9]*))', VERBOSE ) ),
157                 ( 'Name',             ( r'[A-Za-z_][A-Za-z_0-9]*', ) ),
158                 ( 'VariableContents', ( r'''[^"' ;:=>()]+''', ) ),
159                 ( 'EndOfLine',        ( r';', ) ),
160         ]
161
162         # Tokens to filter out of the token stream
163         useless = ['Space', 'Comment']
164
165         tokens = make_tokenizer( specs )
166         return [x for x in tokens( string ) if x.type not in useless]
167
168
169
170 ### Parsing ###
171
172  ## Map Arrays
173 macros_map        = Macros()
174 variables_dict    = Variables()
175 capabilities_dict = Capabilities()
176
177
178  ## Parsing Functions
179
180 def make_scanCode( token ):
181         scanCode = int( token[1:], 0 )
182         # Check size, to make sure it's valid
183         if scanCode > 0xFF:
184                 print ( "{0} ScanCode value {1} is larger than 255".format( ERROR, scanCode ) )
185                 raise
186         return scanCode
187
188 def make_usbCode( token ):
189         # If first character is a U, strip
190         if token[0] == "U":
191                 token = token[1:]
192
193         # If using string representation of USB Code, do lookup, case-insensitive
194         if '"' in token:
195                 try:
196                         usbCode = kll_hid_lookup_dictionary[ token[1:-1].upper() ]
197                 except LookupError as err:
198                         print ( "{0} {1} is an invalid USB Code Lookup...".format( ERROR, err ) )
199                         raise
200         else:
201                 usbCode = int( token, 0 )
202
203         # Check size, to make sure it's valid
204         if usbCode > 0xFF:
205                 print ( "{0} USBCode value {1} is larger than 255".format( ERROR, usbCode ) )
206                 raise
207         return usbCode
208
209 def make_seqString( token ):
210         # Shifted Characters, and amount to move by to get non-shifted version
211         # US ANSI
212         shiftCharacters = (
213                 ( "ABCDEFGHIJKLMNOPQRSTUVWXYZ", 0x20 ),
214                 ( "+",       0x12 ),
215                 ( "&(",      0x11 ),
216                 ( "!#$%<>",  0x10 ),
217                 ( "*",       0x0E ),
218                 ( ")",       0x07 ),
219                 ( '"',       0x05 ),
220                 ( ":",       0x01 ),
221                 ( "^",      -0x10 ),
222                 ( "_",      -0x18 ),
223                 ( "{}|",    -0x1E ),
224                 ( "~",      -0x20 ),
225                 ( "@",      -0x32 ),
226                 ( "?",      -0x38 ),
227         )
228
229         listOfLists = []
230         shiftKey = kll_hid_lookup_dictionary["SHIFT"]
231
232         # Creates a list of USB codes from the string: sequence (list) of combos (lists)
233         for char in token[1:-1]:
234                 processedChar = char
235
236                 # Whether or not to create a combo for this sequence with a shift
237                 shiftCombo = False
238
239                 # Depending on the ASCII character, convert to single character or Shift + character
240                 for pair in shiftCharacters:
241                         if char in pair[0]:
242                                 shiftCombo = True
243                                 processedChar = chr( ord( char ) + pair[1] )
244                                 break
245
246                 # Do KLL HID Lookup on non-shifted character
247                 # NOTE: Case-insensitive, which is why the shift must be pre-computed
248                 usbCode = kll_hid_lookup_dictionary[ processedChar.upper() ]
249
250                 # Create Combo for this character, add shift key if shifted
251                 charCombo = []
252                 if shiftCombo:
253                         charCombo = [ [ shiftKey ] ]
254                 charCombo.append( [ usbCode ] )
255
256                 # Add to list of lists
257                 listOfLists.append( charCombo )
258
259         return listOfLists
260
261 def make_string( token ):
262         return token[1:-1]
263
264 def make_unseqString( token ):
265         return token[1:-1]
266
267 def make_number( token ):
268         return int( token, 0 )
269
270   # Range can go from high to low or low to high
271 def make_scanCode_range( rangeVals ):
272         start = rangeVals[0]
273         end   = rangeVals[1]
274
275         # Swap start, end if start is greater than end
276         if start > end:
277                 start, end = end, start
278
279         # Iterate from start to end, and generate the range
280         return list( range( start, end + 1 ) )
281
282   # Range can go from high to low or low to high
283   # Warn on 0-9 (as this does not do what one would expect) TODO
284   # Lookup USB HID tags and convert to a number
285 def make_usbCode_range( rangeVals ):
286         # Check if already integers
287         if isinstance( rangeVals[0], int ):
288                 start = rangeVals[0]
289         else:
290                 start = make_usbCode( rangeVals[0] )
291
292         if isinstance( rangeVals[1], int ):
293                 end = rangeVals[1]
294         else:
295                 end = make_usbCode( rangeVals[1] )
296
297         # Swap start, end if start is greater than end
298         if start > end:
299                 start, end = end, start
300
301         # Iterate from start to end, and generate the range
302         return list( range( start, end + 1 ) )
303         pass
304
305
306  ## Base Rules
307
308 const       = lambda x: lambda _: x
309 unarg       = lambda f: lambda x: f(*x)
310 flatten     = lambda list: sum( list, [] )
311
312 tokenValue  = lambda x: x.value
313 tokenType   = lambda t: some( lambda x: x.type == t ) >> tokenValue
314 operator    = lambda s: a( Token( 'Operator', s ) ) >> tokenValue
315 parenthesis = lambda s: a( Token( 'Parenthesis', s ) ) >> tokenValue
316 eol         = a( Token( 'EndOfLine', ';' ) )
317
318 def listElem( item ):
319         return [ item ]
320
321 def listToTuple( items ):
322         return tuple( items )
323
324   # Flatten only the top layer (list of lists of ...)
325 def oneLayerFlatten( items ):
326         mainList = []
327         for sublist in items:
328                 for item in sublist:
329                         mainList.append( item )
330
331         return mainList
332
333   # Capability arguments may need to be expanded (e.g. 1 16 bit argument needs to be 2 8 bit arguments for the state machine)
334 def capArgExpander( items ):
335         newArgs = []
336         # For each defined argument in the capability definition
337         for arg in range( 0, len( capabilities_dict[ items[0] ][1] ) ):
338                 argLen = capabilities_dict[ items[0] ][1][ arg ][1]
339                 num = items[1][ arg ]
340                 byteForm = num.to_bytes( argLen, byteorder='little' ) # XXX Yes, little endian from how the uC structs work
341
342                 # For each sub-argument, split into byte-sized chunks
343                 for byte in range( 0, argLen ):
344                         newArgs.append( byteForm[ byte ] )
345
346         return tuple( [ items[0], tuple( newArgs ) ] )
347
348   # Expand ranges of values in the 3rd dimension of the list, to a list of 2nd lists
349   # i.e. [ sequence, [ combo, [ range ] ] ] --> [ [ sequence, [ combo ] ], <option 2>, <option 3> ]
350 def optionExpansion( sequences ):
351         expandedSequences = []
352
353         # Total number of combinations of the sequence of combos that needs to be generated
354         totalCombinations = 1
355
356         # List of leaf lists, with number of leaves
357         maxLeafList = []
358
359         # Traverse to the leaf nodes, and count the items in each leaf list
360         for sequence in sequences:
361                 for combo in sequence:
362                         rangeLen = len( combo )
363                         totalCombinations *= rangeLen
364                         maxLeafList.append( rangeLen )
365
366         # Counter list to keep track of which combination is being generated
367         curLeafList = [0] * len( maxLeafList )
368
369         # Generate a list of permuations of the sequence of combos
370         for count in range( 0, totalCombinations ):
371                 expandedSequences.append( [] ) # Prepare list for adding the new combination
372                 position = 0
373
374                 # Traverse sequence of combos to generate permuation
375                 for sequence in sequences:
376                         expandedSequences[ -1 ].append( [] )
377                         for combo in sequence:
378                                 expandedSequences[ -1 ][ -1 ].append( combo[ curLeafList[ position ] ] )
379                                 position += 1
380
381                 # Increment combination tracker
382                 for leaf in range( 0, len( curLeafList ) ):
383                         curLeafList[ leaf ] += 1
384
385                         # Reset this position, increment next position (if it exists), then stop
386                         if curLeafList[ leaf ] >= maxLeafList[ leaf ]:
387                                 curLeafList[ leaf ] = 0
388                                 if leaf + 1 < len( curLeafList ):
389                                         curLeafList[ leaf + 1 ] += 1
390
391         return expandedSequences
392
393
394 # Converts USB Codes into Capabilities
395 def usbCodeToCapability( items ):
396         # Items already converted to variants using optionExpansion
397         for variant in range( 0, len( items ) ):
398                 # Sequence of Combos
399                 for sequence in range( 0, len( items[ variant ] ) ):
400                         for combo in range( 0, len( items[ variant ][ sequence ] ) ):
401                                 # Only convert if an integer, otherwise USB Code doesn't need converting
402                                 if isinstance( items[ variant ][ sequence ][ combo ], int ):
403                                         # Use backend capability name and a single argument
404                                         items[ variant ][ sequence ][ combo ] = tuple( [ backend.usbCodeCapability(), tuple( [ items[ variant ][ sequence ][ combo ] ] ) ] )
405
406         return items
407
408
409  ## Evaluation Rules
410
411 def eval_scanCode( triggers, operator, results ):
412         # Convert to lists of lists of lists to tuples of tuples of tuples
413         # Tuples are non-mutable, and can be used has index items
414         triggers = tuple( tuple( tuple( sequence ) for sequence in variant ) for variant in triggers )
415         results  = tuple( tuple( tuple( sequence ) for sequence in variant ) for variant in results )
416
417         # Iterate over all combinations of triggers and results
418         for trigger in triggers:
419                 for result in results:
420                         # Append Case
421                         if operator == ":+":
422                                 macros_map.appendScanCode( trigger, result )
423
424                         # Remove Case
425                         elif operator == ":-":
426                                 macros_map.removeScanCode( trigger, result )
427
428                         # Replace Case
429                         elif operator == ":":
430                                 macros_map.replaceScanCode( trigger, result )
431
432 def eval_usbCode( triggers, operator, results ):
433         # Convert to lists of lists of lists to tuples of tuples of tuples
434         # Tuples are non-mutable, and can be used has index items
435         triggers = tuple( tuple( tuple( sequence ) for sequence in variant ) for variant in triggers )
436         results  = tuple( tuple( tuple( sequence ) for sequence in variant ) for variant in results )
437
438         # Iterate over all combinations of triggers and results
439         for trigger in triggers:
440                 scanCodes = macros_map.lookupUSBCodes( trigger )
441                 for scanCode in scanCodes:
442                         for result in results:
443                                 # Cache assignment until file finishes processing
444                                 macros_map.cacheAssignment( operator, scanCode, result )
445
446 def eval_variable( name, content ):
447         # Content might be a concatenation of multiple data types, convert everything into a single string
448         assigned_content = ""
449         for item in content:
450                 assigned_content += str( item )
451
452         variables_dict.assignVariable( name, assigned_content )
453
454 def eval_capability( name, function, args ):
455         capabilities_dict[ name ] = [ function, args ]
456
457 def eval_define( name, cdefine_name ):
458         variables_dict.defines[ name ] = cdefine_name
459
460 map_scanCode   = unarg( eval_scanCode )
461 map_usbCode    = unarg( eval_usbCode )
462
463 set_variable   = unarg( eval_variable )
464 set_capability = unarg( eval_capability )
465 set_define     = unarg( eval_define )
466
467
468  ## Sub Rules
469
470 usbCode     = tokenType('USBCode') >> make_usbCode
471 scanCode    = tokenType('ScanCode') >> make_scanCode
472 name        = tokenType('Name')
473 number      = tokenType('Number') >> make_number
474 comma       = tokenType('Comma')
475 dash        = tokenType('Dash')
476 plus        = tokenType('Plus')
477 content     = tokenType('VariableContents')
478 string      = tokenType('String') >> make_string
479 unString    = tokenType('String') # When the double quotes are still needed for internal processing
480 seqString   = tokenType('SequenceString') >> make_seqString
481 unseqString = tokenType('SequenceString') >> make_unseqString # For use with variables
482
483   # Code variants
484 code_end = tokenType('CodeEnd')
485
486   # Scan Codes
487 scanCode_start     = tokenType('ScanCodeStart')
488 scanCode_range     = number + skip( dash ) + number >> make_scanCode_range
489 scanCode_listElem  = number >> listElem
490 scanCode_innerList = oneplus( ( scanCode_range | scanCode_listElem ) + skip( maybe( comma ) ) ) >> flatten
491 scanCode_expanded  = skip( scanCode_start ) + scanCode_innerList + skip( code_end )
492 scanCode_elem      = scanCode >> listElem
493 scanCode_combo     = oneplus( ( scanCode_expanded | scanCode_elem ) + skip( maybe( plus ) ) )
494 scanCode_sequence  = oneplus( scanCode_combo + skip( maybe( comma ) ) )
495
496   # USB Codes
497 usbCode_start       = tokenType('USBCodeStart')
498 usbCode_range       = ( number | unString ) + skip( dash ) + ( number | unString ) >> make_usbCode_range
499 usbCode_listElemTag = unString >> make_usbCode
500 usbCode_listElem    = ( number | usbCode_listElemTag ) >> listElem
501 usbCode_innerList   = oneplus( ( usbCode_range | usbCode_listElem ) + skip( maybe( comma ) ) ) >> flatten
502 usbCode_expanded    = skip( usbCode_start ) + usbCode_innerList + skip( code_end )
503 usbCode_elem        = usbCode >> listElem
504 usbCode_combo       = oneplus( ( usbCode_expanded | usbCode_elem ) + skip( maybe( plus ) ) ) >> listElem
505 usbCode_sequence    = oneplus( ( usbCode_combo | seqString ) + skip( maybe( comma ) ) ) >> oneLayerFlatten
506
507   # Capabilities
508 capFunc_arguments = many( number + skip( maybe( comma ) ) ) >> listToTuple
509 capFunc_elem      = name + skip( parenthesis('(') ) + capFunc_arguments + skip( parenthesis(')') ) >> capArgExpander >> listElem
510 capFunc_combo     = oneplus( ( usbCode_expanded | usbCode_elem | capFunc_elem ) + skip( maybe( plus ) ) ) >> listElem
511 capFunc_sequence  = oneplus( ( capFunc_combo | seqString ) + skip( maybe( comma ) ) ) >> oneLayerFlatten
512
513   # Trigger / Result Codes
514 triggerCode_outerList    = scanCode_sequence >> optionExpansion
515 triggerUSBCode_outerList = usbCode_sequence >> optionExpansion >> usbCodeToCapability
516 resultCode_outerList     = capFunc_sequence >> optionExpansion >> usbCodeToCapability
517
518
519  ## Main Rules
520
521 #| <variable> = <variable contents>;
522 variable_contents   = name | content | string | number | comma | dash | unseqString
523 variable_expression = name + skip( operator('=') ) + oneplus( variable_contents ) + skip( eol ) >> set_variable
524
525 #| <capability name> => <c function>;
526 capability_arguments  = name + skip( operator(':') ) + number + skip( maybe( comma ) )
527 capability_expression = name + skip( operator('=>') ) + name + skip( parenthesis('(') ) + many( capability_arguments ) + skip( parenthesis(')') ) + skip( eol ) >> set_capability
528
529 #| <define name> => <c define>;
530 define_expression = name + skip( operator('=>') ) + name + skip( eol ) >> set_define
531
532 #| <trigger> : <result>;
533 operatorTriggerResult = operator(':') | operator(':+') | operator(':-')
534 scanCode_expression   = triggerCode_outerList + operatorTriggerResult + resultCode_outerList + skip( eol ) >> map_scanCode
535 usbCode_expression    = triggerUSBCode_outerList + operatorTriggerResult + resultCode_outerList + skip( eol ) >> map_usbCode
536
537 def parse( tokenSequence ):
538         """Sequence(Token) -> object"""
539
540         # Top-level Parser
541         expression = scanCode_expression | usbCode_expression | variable_expression | capability_expression | define_expression
542
543         kll_text = many( expression )
544         kll_file = maybe( kll_text ) + skip( finished )
545
546         return kll_file.parse( tokenSequence )
547
548
549
550 def processKLLFile( filename ):
551         with open( filename ) as file:
552                 data = file.read()
553                 tokenSequence = tokenize( data )
554                 #print ( pformat( tokenSequence ) ) # Display tokenization
555                 try:
556                         tree = parse( tokenSequence )
557                 except NoParseError as e:
558                         print("Error parsing %s. %s" % (filename, e.msg), file=sys.stderr)
559                         sys.exit(1)
560
561
562 ### Misc Utility Functions ###
563
564 def gitRevision( kllPath ):
565         import subprocess
566
567         # Change the path to where kll.py is
568         origPath = os.getcwd()
569         os.chdir( kllPath )
570
571         # Just in case git can't be found
572         try:
573                 # Get hash of the latest git commit
574                 revision = subprocess.check_output( ['git', 'rev-parse', 'HEAD'] ).decode()[:-1]
575
576                 # Get list of files that have changed since the commit
577                 changed = subprocess.check_output( ['git', 'diff-index', '--name-only', 'HEAD', '--'] ).decode().splitlines()
578         except:
579                 revision = "<no git>"
580                 changed = []
581
582         # Change back to the old working directory
583         os.chdir( origPath )
584
585         return revision, changed
586
587
588 ### Main Entry Point ###
589
590 if __name__ == '__main__':
591         (baseFiles, defaultFiles, partialFileSets, backend_name, template, defines_template, output, defines_output) = processCommandLineArgs()
592
593         # Look up git information on the compiler
594         gitRev, gitChanges = gitRevision( os.path.dirname( os.path.realpath( __file__ ) ) )
595
596         # Load backend module
597         global backend
598         backend_import = importlib.import_module( "backends.{0}".format( backend_name ) )
599         backend = backend_import.Backend( template, defines_template )
600
601         # Process base layout files
602         for filename in baseFiles:
603                 variables_dict.setCurrentFile( filename )
604                 processKLLFile( filename )
605         macros_map.completeBaseLayout() # Indicates to macros_map that the base layout is complete
606         variables_dict.baseLayoutFinished()
607
608         # Default combined layer
609         for filename in defaultFiles:
610                 variables_dict.setCurrentFile( filename )
611                 processKLLFile( filename )
612                 # Apply assignment cache, see 5.1.2 USB Codes for why this is necessary
613                 macros_map.replayCachedAssignments()
614
615         # Iterate through additional layers
616         for partial in partialFileSets:
617                 # Increment layer for each -p option
618                 macros_map.addLayer()
619                 variables_dict.incrementLayer() # DefaultLayer is layer 0
620
621                 # Iterate and process each of the file in the layer
622                 for filename in partial:
623                         variables_dict.setCurrentFile( filename )
624                         processKLLFile( filename )
625
626                 # Apply assignment cache, see 5.1.2 USB Codes for why this is necessary
627                 macros_map.replayCachedAssignments()
628                 # Remove un-marked keys to complete the partial layer
629                 macros_map.removeUnmarked()
630
631         # Do macro correlation and transformation
632         macros_map.generate()
633
634         # Process needed templating variables using backend
635         backend.process(
636                 capabilities_dict,
637                 macros_map,
638                 variables_dict,
639                 gitRev,
640                 gitChanges
641         )
642
643         # Generate output file using template and backend
644         backend.generate( output, defines_output )
645
646         # Successful Execution
647         sys.exit( 0 )
648