@@ -337,7 +337,7 @@ def tailTo(
337337 out : list [str ],
338338 node : IWrap [_frontend .node .Node ],
339339 noAdvance : bool ,
340- value : Optional [int ],
340+ value : Optional [int ] = None ,
341341 ):
342342 ctx = self .compilation
343343 target = ctx .unwrapNode (node ).build (ctx )
@@ -512,7 +512,8 @@ def __init__(self, ref: _frontend.node.Sequence) -> None:
512512
513513 def doBuild (self , out : list [str ]):
514514 ctx = self .compilation
515-
515+ # TODO: llparse_match_t could be easily changed around to
516+ # Something that can't be overlapped with when compiled with other parsers...
516517 out .append ("llparse_match_t match_seq;" )
517518 out .append ("" )
518519
@@ -639,7 +640,7 @@ def doBuild(self, out: list[str]):
639640 # Invoke callback
640641 callback = ctx .buildCode (ctx .unwrapCode (self .ref .callback , True ))
641642
642- out .append (f"err = { callback } ({ ctx .stateArg ()} , start,{ ctx .posArg ()} );" )
643+ out .append (f"err = { callback } ({ ctx .stateArg ()} , start, { ctx .posArg ()} );" )
643644
644645 out .append ("if (err != 0) {" )
645646 tmp = []
@@ -676,6 +677,163 @@ def buildError(self, out: list[str], code: str):
676677 out .append (f"return { STATE_ERROR } ;" )
677678
678679
680+ # Based off arthurschreiber's work with Indutny's Tips and requests added to the mix.
681+
682+ # 0x80 I8
683+ # 0x8000 I16
684+ # 0x800000 I24
685+ # 0x1000000 U24
686+
687+ class Int (Node ):
688+ def __init__ (self , ref : _frontend .node .Int ):
689+ super ().__init__ (ref )
690+ self .ref = ref
691+ self .offset = ref .byteOffset
692+ # I'm going to deviate from arthurschreiber's work a bit with indutny's suggestions.
693+ # we should really be using bitwise operators like rshift and lshift
694+ @property
695+ def pair (self ):
696+ return self .compilation , self .compilation .stateField (self .ref .field )
697+
698+ def readInt8 (self , out : list [str ]) -> None :
699+ ctx , index = self .pair
700+ out .append (f"{ index } = ((*{ ctx .posArg ()} ) & 0x80);" )
701+
702+ def readUInt8 (self , out : list [str ]) -> None :
703+ ctx , index = self .pair
704+ out .append (f"{ index } = (*{ ctx .posArg ()} );" )
705+
706+ # LITTLE ENDIAN
707+
708+ def readInt16LE (self , out : list [str ]) -> None :
709+ ctx , index = self .pair
710+ if self .offset == 0 :
711+ out .append (f"{ index } = (*{ ctx .posArg ()} );" )
712+ else :
713+ # Since BE Belongs to performing << aka left shifts we do >> right shifts
714+ out .append (f"{ index } = ({ index } >> 8) | ((*{ ctx .posArg ()} ) & 0x80);" )
715+
716+ def readUInt16LE (self , out : list [str ]) -> None :
717+ ctx , index = self .pair
718+ if self .offset == 0 :
719+ out .append (f"{ index } = (*{ ctx .posArg ()} );" )
720+ else :
721+ out .append (f"{ index } = ({ index } >> 8) | (*{ ctx .posArg ()} );" )
722+
723+ def readInt24LE (self , out : list [str ]) -> None :
724+ ctx , index = self .pair
725+ if self .offset == 0 :
726+ out .append (f"{ index } = (*{ ctx .posArg ()} );" )
727+ elif self .offset == 1 :
728+ out .append (f"{ index } = ({ index } >> 8) | (*{ ctx .posArg ()} );" )
729+ else :
730+ out .append (f"{ index } = ({ index } >> 8) | ((*{ ctx .posArg ()} ) & 0x80);" )
731+
732+ def readUInt24LE (self , out : list [str ]) -> None :
733+ ctx , index = self .pair
734+ if self .offset == 0 :
735+ out .append (f"{ index } = (*{ ctx .posArg ()} );" )
736+ else :
737+ out .append (f"{ index } = ({ index } >> 8) | (*{ ctx .posArg ()} );" )
738+
739+ def readInt32LE (self , out : list [str ]) -> None :
740+ ctx , index = self .pair
741+ if self .offset == 0 :
742+ out .append (f"{ index } = (*{ ctx .posArg ()} );" )
743+ elif self .offset in (1 , 2 ):
744+ out .append (f"{ index } = ({ index } >> 8) | (*{ ctx .posArg ()} );" )
745+ else :
746+ out .append (f"{ index } = ({ index } >> 8) | ((*{ ctx .posArg ()} ) & 0x80);" )
747+
748+ def readUInt32LE (self , out : list [str ]) -> None :
749+ ctx , index = self .pair
750+ if self .offset == 0 :
751+ out .append (f"{ index } = (*{ ctx .posArg ()} );" )
752+ else :
753+ out .append (f"{ index } = ({ index } >> 8) | (*{ ctx .posArg ()} );" )
754+
755+ # BIG ENDIAN
756+
757+ def readInt16BE (self , out : list [str ]) -> None :
758+ ctx , index = self .pair
759+ if self .offset == 0 :
760+ out .append (f"{ index } = (*{ ctx .posArg ()} );" )
761+ else :
762+ # Since LE Belongs to >> we do "<<" instead
763+ out .append (f"{ index } = ({ index } << 8) | ((*{ ctx .posArg ()} ) & 0x80);" )
764+
765+ def readUInt16BE (self , out : list [str ]) -> None :
766+ ctx , index = self .pair
767+ if self .offset == 0 :
768+ out .append (f"{ index } = (*{ ctx .posArg ()} );" )
769+ else :
770+ out .append (f"{ index } = ({ index } << 8) | (*{ ctx .posArg ()} );" )
771+
772+ def readInt24BE (self , out : list [str ]) -> None :
773+ ctx , index = self .pair
774+ if self .offset == 0 :
775+ out .append (f"{ index } = (*{ ctx .posArg ()} );" )
776+ elif self .offset == 1 :
777+ out .append (f"{ index } = ({ index } << 8) | (*{ ctx .posArg ()} );" )
778+ else :
779+ out .append (f"{ index } = ({ index } << 8) | ((*{ ctx .posArg ()} ) & 0x80);" )
780+
781+ def readUInt24BE (self , out : list [str ]) -> None :
782+ ctx , index = self .pair
783+ if self .offset == 0 :
784+ out .append (f"{ index } = (*{ ctx .posArg ()} );" )
785+ else :
786+ out .append (f"{ index } = ({ index } << 8) | (*{ ctx .posArg ()} );" )
787+
788+ def readInt32BE (self , out : list [str ]) -> None :
789+ ctx , index = self .pair
790+ if self .offset == 0 :
791+ out .append (f"{ index } = (*{ ctx .posArg ()} );" )
792+ elif self .offset in (1 , 2 ):
793+ out .append (f"{ index } = ({ index } << 8) | (*{ ctx .posArg ()} );" )
794+ else :
795+ out .append (f"{ index } = ({ index } << 8) | ((*{ ctx .posArg ()} ) & 0x80);" )
796+
797+ def readUInt32BE (self , out : list [str ]) -> None :
798+ ctx , index = self .pair
799+ if self .offset == 0 :
800+ out .append (f"{ index } = (*{ ctx .posArg ()} );" )
801+ else :
802+ out .append (f"{ index } = ({ index } << 8) | (*{ ctx .posArg ()} );" )
803+
804+
805+ def doBuild (self , out :list [str ]):
806+ self .prologue (out )
807+ # I'm still supporting 3.9 but I plan to drop it's support in favor of match case soon...
808+ bits = self .ref .bits
809+
810+ if self .compilation .getFieldType (self .ref .field ) == 'ptr' :
811+ raise ValueError (f'property { self .ref .field } should not use pointers but it was given \" ptr\" ' )
812+
813+ if bits == 1 :
814+ self .readInt8 (out ) if self .ref .signed else self .readUInt8 (out )
815+ elif bits == 2 :
816+ if self .ref .littleEndian :
817+ self .readInt16LE (out ) if self .ref .signed else self .readUInt16LE (out )
818+ else :
819+ self .readInt16BE (out ) if self .ref .signed else self .readUInt16BE (out )
820+ elif bits == 3 :
821+ if self .ref .littleEndian :
822+ self .readInt24LE (out ) if self .ref .signed else self .readUInt24LE (out )
823+ else :
824+ self .readInt24BE (out ) if self .ref .signed else self .readUInt24BE (out )
825+ else :
826+ if self .ref .littleEndian :
827+ self .readInt32LE (out ) if self .ref .signed else self .readUInt32LE (out )
828+ else :
829+ self .readInt32BE (out ) if self .ref .signed else self .readUInt32BE (out )
830+ # TODO: uint64 & int64
831+
832+ self .tailTo (out , self .ref .otherwise .node , self .ref .otherwise .noAdvance , None )
833+
834+
835+
836+
679837MAX_CHAR = 0xFF
680838TABLE_GROUP = 16
681839
@@ -1096,6 +1254,8 @@ def unwrapNode(self, node: IWrap[_frontend.node.Node]):
10961254 r = Sequence (ref )
10971255 elif isinstance (ref , _frontend .node .TableLookup ):
10981256 r = TableLookup (ref )
1257+ elif isinstance (ref , _frontend .node .Int ):
1258+ r = Int (ref )
10991259 else :
11001260 raise TypeError (
11011261 f'refrence "{ ref } " is an Invalid Code Type , TypeName:"{ ref .__class__ .__name__ } "'
0 commit comments