Archived
1
0

First pass at parsing rules for KLL 0.4

(Yep, definitely started 0.5 first :P)

- Not functional yet, do not use
This commit is contained in:
Jacob Alexander 2016-01-25 00:33:52 -08:00
parent 9110f81e97
commit c2a798f1cb
2 changed files with 104 additions and 27 deletions

119
kll.py
View File

@ -164,6 +164,7 @@ def tokenize( string ):
( 'Plus', ( r'\+', ) ), ( 'Plus', ( r'\+', ) ),
( 'Parenthesis', ( r'\(|\)', ) ), ( 'Parenthesis', ( r'\(|\)', ) ),
( 'None', ( r'None', ) ), ( 'None', ( r'None', ) ),
( 'Timing', ( r'[0-9]+(.[0-9]+)?((s)|(ms)|(us))', ) ),
( 'Number', ( r'-?(0x[0-9a-fA-F]+)|(0|([1-9][0-9]*))', VERBOSE ) ), ( 'Number', ( r'-?(0x[0-9a-fA-F]+)|(0|([1-9][0-9]*))', VERBOSE ) ),
( 'Name', ( r'[A-Za-z_][A-Za-z_0-9]*', ) ), ( 'Name', ( r'[A-Za-z_][A-Za-z_0-9]*', ) ),
( 'VariableContents', ( r'''[^"' ;:=>()]+''', ) ), ( 'VariableContents', ( r'''[^"' ;:=>()]+''', ) ),
@ -199,8 +200,8 @@ class Make:
return scanCode return scanCode
def hidCode( type, token ): def hidCode( type, token ):
# If first character is a U, strip # If first character is a U or I, strip
if token[0] == "U": if token[0] == "U" or token[0] == "I":
token = token[1:] token = token[1:]
# CONS specifier # CONS specifier
elif 'CONS' in token: elif 'CONS' in token:
@ -237,6 +238,8 @@ class Make:
return Make.consCode_number( hidCode ) return Make.consCode_number( hidCode )
elif type == 'SysCode': elif type == 'SysCode':
return Make.sysCode_number( hidCode ) return Make.sysCode_number( hidCode )
elif type == 'IndCode':
return Make.indCode_number( hidCode )
print ( "{0} Unknown HID Specifier '{1}'".format( ERROR, type ) ) print ( "{0} Unknown HID Specifier '{1}'".format( ERROR, type ) )
raise raise
@ -250,6 +253,9 @@ class Make:
def sysCode( token ): def sysCode( token ):
return Make.hidCode( 'SysCode', token ) return Make.hidCode( 'SysCode', token )
def indCode( token ):
return Make.hidCode( 'IndCode', token )
def animation( token ): def animation( token ):
# TODO # TODO
print( token ) print( token )
@ -311,6 +317,7 @@ class Make:
'ConsCode' : 'CONS', 'ConsCode' : 'CONS',
'SysCode' : 'SYS', 'SysCode' : 'SYS',
'USBCode' : 'USB', 'USBCode' : 'USB',
'IndCode' : 'LED',
} }
return ( lookup[ type ], token ) return ( lookup[ type ], token )
@ -323,6 +330,9 @@ class Make:
def sysCode_number( token ): def sysCode_number( token ):
return Make.hidCode_number( 'SysCode', token ) return Make.hidCode_number( 'SysCode', token )
def indCode_number( token ):
return Make.hidCode_number( 'IndCode', token )
# Replace key-word with None specifier (which indicates a noneOut capability) # Replace key-word with None specifier (which indicates a noneOut capability)
def none( token ): def none( token ):
return [[[('NONE', 0)]]] return [[[('NONE', 0)]]]
@ -388,6 +398,44 @@ class Make:
def number( token ): def number( token ):
return int( token, 0 ) return int( token, 0 )
def timing( token ):
# Find ms, us, or s
if 'ms' in token:
unit = 'ms'
num = token.split('m')[0]
print (token.split('m'))
elif 'us' in token:
unit = 'us'
num = token.split('u')[0]
elif 's' in token:
unit = 's'
num = token.split('s')[0]
else:
print ( "{0} cannot find timing unit in token '{1}'".format( ERROR, token ) )
return "NULL"
print ( num, unit )
ret = {
'time' : float( num ),
'unit' : unit,
}
return ret
def specifierState( values ):
# TODO
print ( values )
return "SPECSTATE"
def specifierAnalog( value ):
# TODO
print( value )
return "SPECANALOG"
def specifierUnroll( value ):
# TODO
print( value )
return [ value[0] ]
# Range can go from high to low or low to high # Range can go from high to low or low to high
def scanCode_range( rangeVals ): def scanCode_range( rangeVals ):
start = rangeVals[0] start = rangeVals[0]
@ -436,6 +484,9 @@ class Make:
def consCode_range( rangeVals ): def consCode_range( rangeVals ):
return Make.hidCode_range( 'ConsCode', rangeVals ) return Make.hidCode_range( 'ConsCode', rangeVals )
def indCode_range( rangeVals ):
return Make.hidCode_range( 'IndCode', rangeVals )
def range( rangeVals ): def range( rangeVals ):
# TODO # TODO
print (rangeVals) print (rangeVals)
@ -565,6 +616,7 @@ def tupleit( t ):
class Eval: class Eval:
def scanCode( triggers, operator, results ): def scanCode( triggers, operator, results ):
print ( triggers, operator, results )
# Convert to lists of lists of lists to tuples of tuples of tuples # Convert to lists of lists of lists to tuples of tuples of tuples
# Tuples are non-mutable, and can be used has index items # Tuples are non-mutable, and can be used has index items
triggers = tuple( tuple( tuple( sequence ) for sequence in variant ) for variant in triggers ) triggers = tuple( tuple( tuple( sequence ) for sequence in variant ) for variant in triggers )
@ -605,6 +657,8 @@ class Eval:
macros_map.replaceScanCode( trigger, result ) macros_map.replaceScanCode( trigger, result )
def usbCode( triggers, operator, results ): def usbCode( triggers, operator, results ):
# TODO
return
# Convert to lists of lists of lists to tuples of tuples of tuples # Convert to lists of lists of lists to tuples of tuples of tuples
# Tuples are non-mutable, and can be used has index items # Tuples are non-mutable, and can be used has index items
triggers = tuple( tuple( tuple( sequence ) for sequence in variant ) for variant in triggers ) triggers = tuple( tuple( tuple( sequence ) for sequence in variant ) for variant in triggers )
@ -698,6 +752,7 @@ usbCode = tokenType('USBCode') >> Make.usbCode
scanCode = tokenType('ScanCode') >> Make.scanCode scanCode = tokenType('ScanCode') >> Make.scanCode
consCode = tokenType('ConsCode') >> Make.consCode consCode = tokenType('ConsCode') >> Make.consCode
sysCode = tokenType('SysCode') >> Make.sysCode sysCode = tokenType('SysCode') >> Make.sysCode
indCode = tokenType('Indicator') >> Make.indCode
animation = tokenType('Animation') >> Make.animation animation = tokenType('Animation') >> Make.animation
pixel = tokenType('Pixel') >> Make.pixel pixel = tokenType('Pixel') >> Make.pixel
pixelLayer = tokenType('PixelLayer') >> Make.pixelLayer pixelLayer = tokenType('PixelLayer') >> Make.pixelLayer
@ -705,6 +760,7 @@ none = tokenType('None') >> Make.none
position = tokenType('Position') >> Make.position position = tokenType('Position') >> Make.position
name = tokenType('Name') name = tokenType('Name')
number = tokenType('Number') >> Make.number number = tokenType('Number') >> Make.number
timing = tokenType('Timing') >> Make.timing
comma = tokenType('Comma') comma = tokenType('Comma')
dash = tokenType('Dash') dash = tokenType('Dash')
plus = tokenType('Plus') plus = tokenType('Plus')
@ -719,37 +775,32 @@ pixelOperator = tokenType('PixelOperator')
code_begin = tokenType('CodeBegin') code_begin = tokenType('CodeBegin')
code_end = tokenType('CodeEnd') code_end = tokenType('CodeEnd')
# Specifier
specifier_state = ( name + skip( operator(':') ) + timing ) | ( name + skip( operator(':') ) + timing ) | timing | name >> Make.specifierState
specifier_analog = number >> Make.specifierAnalog
specifier_list = skip( parenthesis('(') ) + many( ( specifier_state | specifier_analog ) + skip( maybe( comma ) ) ) + skip( parenthesis(')') )
# Scan Codes # Scan Codes
scanCode_start = tokenType('ScanCodeStart') scanCode_start = tokenType('ScanCodeStart')
scanCode_range = number + skip( dash ) + number >> Make.scanCode_range scanCode_range = number + skip( dash ) + number >> Make.scanCode_range
scanCode_listElem = number >> listElem scanCode_listElem = number >> listElem
scanCode_innerList = oneplus( ( scanCode_range | scanCode_listElem ) + skip( maybe( comma ) ) ) >> flatten scanCode_specifier = ( scanCode_range | scanCode_listElem ) + maybe( specifier_list ) >> Make.specifierUnroll >> flatten
scanCode_innerList = many( scanCode_specifier + skip( maybe( comma ) ) ) >> flatten
scanCode_expanded = skip( scanCode_start ) + scanCode_innerList + skip( code_end ) scanCode_expanded = skip( scanCode_start ) + scanCode_innerList + skip( code_end )
scanCode_elem = scanCode >> listElem scanCode_elem = scanCode + maybe( specifier_list ) >> Make.specifierUnroll >> listElem
scanCode_combo = oneplus( ( scanCode_expanded | scanCode_elem ) + skip( maybe( plus ) ) ) scanCode_combo = oneplus( ( scanCode_expanded | scanCode_elem ) + skip( maybe( plus ) ) )
scanCode_sequence = oneplus( scanCode_combo + skip( maybe( comma ) ) ) scanCode_sequence = oneplus( scanCode_combo + skip( maybe( comma ) ) )
# USB Codes
usbCode_start = tokenType('USBCodeStart')
usbCode_number = number >> Make.usbCode_number
usbCode_range = ( usbCode_number | unString ) + skip( dash ) + ( number | unString ) >> Make.usbCode_range
usbCode_listElemTag = unString >> Make.usbCode
usbCode_listElem = ( usbCode_number | usbCode_listElemTag ) >> listElem
usbCode_innerList = oneplus( ( usbCode_range | usbCode_listElem ) + skip( maybe( comma ) ) ) >> flatten
usbCode_expanded = skip( usbCode_start ) + usbCode_innerList + skip( code_end )
usbCode_elem = usbCode >> listElem
usbCode_combo = oneplus( ( usbCode_expanded | usbCode_elem ) + skip( maybe( plus ) ) ) >> listElem
usbCode_sequence = oneplus( ( usbCode_combo | seqString ) + skip( maybe( comma ) ) ) >> oneLayerFlatten
# Cons Codes # Cons Codes
consCode_start = tokenType('ConsCodeStart') consCode_start = tokenType('ConsCodeStart')
consCode_number = number >> Make.consCode_number consCode_number = number >> Make.consCode_number
consCode_range = ( consCode_number | unString ) + skip( dash ) + ( number | unString ) >> Make.consCode_range consCode_range = ( consCode_number | unString ) + skip( dash ) + ( number | unString ) >> Make.consCode_range
consCode_listElemTag = unString >> Make.consCode consCode_listElemTag = unString >> Make.consCode
consCode_listElem = ( consCode_number | consCode_listElemTag ) >> listElem consCode_listElem = ( consCode_number | consCode_listElemTag ) >> listElem
consCode_innerList = oneplus( ( consCode_range | consCode_listElem ) + skip( maybe( comma ) ) ) >> flatten consCode_specifier = ( consCode_range | consCode_listElem ) + maybe( specifier_list ) >> Make.specifierUnroll >> flatten
consCode_innerList = oneplus( consCode_specifier + skip( maybe( comma ) ) ) >> flatten
consCode_expanded = skip( consCode_start ) + consCode_innerList + skip( code_end ) consCode_expanded = skip( consCode_start ) + consCode_innerList + skip( code_end )
consCode_elem = consCode >> listElem consCode_elem = consCode + maybe( specifier_list ) >> Make.specifierUnroll >> listElem
# Sys Codes # Sys Codes
sysCode_start = tokenType('SysCodeStart') sysCode_start = tokenType('SysCodeStart')
@ -757,12 +808,38 @@ sysCode_number = number >> Make.sysCode_number
sysCode_range = ( sysCode_number | unString ) + skip( dash ) + ( number | unString ) >> Make.sysCode_range sysCode_range = ( sysCode_number | unString ) + skip( dash ) + ( number | unString ) >> Make.sysCode_range
sysCode_listElemTag = unString >> Make.sysCode sysCode_listElemTag = unString >> Make.sysCode
sysCode_listElem = ( sysCode_number | sysCode_listElemTag ) >> listElem sysCode_listElem = ( sysCode_number | sysCode_listElemTag ) >> listElem
sysCode_innerList = oneplus( ( sysCode_range | sysCode_listElem ) + skip( maybe( comma ) ) ) >> flatten sysCode_specifier = ( sysCode_range | sysCode_listElem ) + maybe( specifier_list ) >> Make.specifierUnroll >> flatten
sysCode_innerList = oneplus( sysCode_specifier + skip( maybe( comma ) ) ) >> flatten
sysCode_expanded = skip( sysCode_start ) + sysCode_innerList + skip( code_end ) sysCode_expanded = skip( sysCode_start ) + sysCode_innerList + skip( code_end )
sysCode_elem = sysCode >> listElem sysCode_elem = sysCode + maybe( specifier_list ) >> Make.specifierUnroll >> listElem
# Indicator Codes
indCode_start = tokenType('IndicatorStart')
indCode_number = number >> Make.indCode_number
indCode_range = ( indCode_number | unString ) + skip( dash ) + ( number | unString ) >> Make.indCode_range
indCode_listElemTag = unString >> Make.indCode
indCode_listElem = ( indCode_number | indCode_listElemTag ) >> listElem
indCode_specifier = ( indCode_range | indCode_listElem ) + maybe( specifier_list ) >> Make.specifierUnroll >> flatten
indCode_innerList = oneplus( indCode_specifier + skip( maybe( comma ) ) ) >> flatten
indCode_expanded = skip( indCode_start ) + indCode_innerList + skip( code_end )
indCode_elem = indCode + maybe( specifier_list ) >> Make.specifierUnroll >> listElem
# USB Codes
usbCode_start = tokenType('USBCodeStart')
usbCode_number = number >> Make.usbCode_number
usbCode_range = ( usbCode_number | unString ) + skip( dash ) + ( number | unString ) >> Make.usbCode_range
usbCode_listElemTag = unString >> Make.usbCode
usbCode_listElem = ( usbCode_number | usbCode_listElemTag ) >> listElem
usbCode_specifier = ( usbCode_range | usbCode_listElem ) + maybe( specifier_list ) >> Make.specifierUnroll >> flatten
usbCode_innerList = oneplus( usbCode_specifier + skip( maybe( comma ) ) ) >> flatten
usbCode_expanded = skip( usbCode_start ) + usbCode_innerList + skip( code_end )
usbCode_elem = usbCode + maybe( specifier_list ) >> Make.specifierUnroll >> listElem
# HID Codes # HID Codes
hidCode_elem = usbCode_expanded | usbCode_elem | sysCode_expanded | sysCode_elem | consCode_expanded | consCode_elem hidCode_elem = usbCode_expanded | usbCode_elem | sysCode_expanded | sysCode_elem | consCode_expanded | consCode_elem | indCode_expanded | indCode_elem
usbCode_combo = oneplus( hidCode_elem + skip( maybe( plus ) ) ) >> listElem
usbCode_sequence = oneplus( ( usbCode_combo | seqString ) + skip( maybe( comma ) ) ) >> oneLayerFlatten
# Pixels # Pixels
pixel_start = tokenType('PixelStart') pixel_start = tokenType('PixelStart')

View File

@ -1074,7 +1074,7 @@ kll_hid_lookup_dictionary['USBCode'] = dict([
# USB HID LED Codes # USB HID LED Codes
kll_hid_lookup_dictionary['LEDCode'] = dict([ kll_hid_lookup_dictionary['IndCode'] = dict([
( 'UNDEFINED', ('LED', 0x00) ), ( 'UNDEFINED', ('LED', 0x00) ),
( 'NUMLOCK', ('LED', 0x01) ), ( 'NUMLOCK', ('LED', 0x01) ),
( 'CAPSLOCK', ('LED', 0x02) ), ( 'CAPSLOCK', ('LED', 0x02) ),