@@ -1547,86 +1547,91 @@ def CacheScript(self, name, path = None, line = None):
1547
1547
fullPath = self .GetPath (name )
1548
1548
if not fullPath :
1549
1549
self .Abort ("Could not find the '%s' script." % name , line )
1550
- with open (fullPath ) as f :
1551
- scriptContents = f .read ()
1552
- lines = []
1553
- tokens = []
1554
- try :
1555
- for token in self .lex .Process (scriptContents ):
1556
- if token .type == self .lex .NEWLINE :
1557
- if tokens :
1558
- lines .append (tokens )
1559
- tokens = []
1560
- elif token .type != self .COMMENT_LINE and token .type != self .COMMENT_BLOCK :
1561
- tokens .append (token )
1562
- except LexicalError as e :
1563
- self .Abort ("Found a lexical error in the '%s' script." % name )
1564
- extends = []
1565
- functions = {}
1566
- properties = {}
1567
- states = {}
1568
- statements = []
1569
- try :
1570
- for line in lines :
1571
- stat = self .syn .Process (line )
1572
- if stat :
1573
- statements .append (stat )
1574
- except SyntacticError as e :
1575
- self .Abort ("Found a syntactic error in the '%s' script." % name )
1576
- header = False
1577
- if statements [0 ].type == self .STAT_SCRIPTHEADER :
1578
- if statements [0 ].data .parent :
1579
- header = True
1580
- extends = self .GetLineage (statements [0 ].data .parent )
1581
- parent = self .GetCachedScript (statements [0 ].data .parent )
1582
- functions .update (parent .functions )
1583
- properties .update (parent .properties )
1584
- states .update (parent .states )
1585
- functions ["GOTOSTATE" ] = Statement (self .STAT_FUNCTIONDEF , 0 , FunctionDef (None , None , False , "GOTOSTATE" , "GoToState" , [ParameterDef (self .KW_STRING , "String" , False , "ASNEWSTATE" , "asNewState" , None )], []))
1586
- functions ["GETSTATE" ] = Statement (self .STAT_FUNCTIONDEF , 0 , FunctionDef (self .KW_STRING , "String" , False , "GETSTATE" , "GetState" , [], []))
1587
- functions ["ONINIT" ] = Statement (self .STAT_EVENTDEF , 0 , EventDef (None , "ONINIT" , "OnInit" , [], []))
1588
- functions ["ONBEGINSTATE" ] = Statement (self .STAT_EVENTDEF , 0 , EventDef (None , "ONBEGINSTATE" , "OnBeginState" , [], []))
1589
- functions ["ONENDSTATE" ] = Statement (self .STAT_EVENTDEF , 0 , EventDef (None , "ONENDSTATE" , "OnEndState" , [], []))
1590
- i = 0
1591
- while i < len (statements ):
1592
- if statements [i ].type == self .STAT_FUNCTIONDEF or statements [i ].type == self .STAT_EVENTDEF :
1593
- start = statements [i ]
1594
- functions [statements [i ].data .name ] = start
1595
- if not self .KW_NATIVE in statements [i ].data .flags :
1596
- while i < len (statements ) and not (statements [i ].type == self .STAT_KEYWORD and (statements [i ].data .type == self .KW_ENDFUNCTION or statements [i ].data .type == self .KW_ENDEVENT )):
1597
- if start .type == self .STAT_FUNCTIONDEF and not start .data .docstring :
1598
- if statements [i ].type == self .STAT_DOCUMENTATION :
1599
- start .data .docstring = statements [i ]
1600
- i += 1
1601
- else :
1602
- if start .type == self .STAT_FUNCTIONDEF and i + 1 < len (statements ) and statements [i + 1 ].type == self .STAT_DOCUMENTATION :
1603
- start .data .docstring = statements [i + 1 ]
1604
- i += 1
1605
- elif statements [i ].type == self .STAT_PROPERTYDEF :
1606
- start = statements [i ]
1607
- properties [statements [i ].data .name ] = start
1608
- if not self .KW_AUTO in statements [i ].data .flags and not self .KW_AUTOREADONLY in statements [i ].data .flags :
1609
- while i < len (statements ) and not (statements [i ].type == self .STAT_KEYWORD and statements [i ].data .type == self .KW_ENDPROPERTY ):
1610
- if not start .data .docstring :
1611
- if statements [i ].type == self .STAT_DOCUMENTATION :
1612
- start .data .docstring = statements [i ]
1613
- i += 1
1614
- else :
1615
- if i + 1 < len (statements ) and statements [i + 1 ].type == self .STAT_DOCUMENTATION :
1616
- start .data .docstring = statements [i + 1 ]
1617
- i += 1
1618
- elif statements [i ].type == self .STAT_STATEDEF :
1619
- start = statements [i ]
1620
- states [statements [i ].data .name ] = start
1621
- while i < len (statements ) and not (statements [i ].type == self .STAT_KEYWORD and statements [i ].data .type == self .KW_ENDSTATE ):
1550
+ scriptContents = ""
1551
+ try :
1552
+ with open (fullPath ) as f :
1553
+ scriptContents = f .read ()
1554
+ except UnicodeDecodeError :
1555
+ with open (fullPath , encoding = "utf8" ) as f :
1556
+ scriptContents = f .read ()
1557
+ lines = []
1558
+ tokens = []
1559
+ try :
1560
+ for token in self .lex .Process (scriptContents ):
1561
+ if token .type == self .lex .NEWLINE :
1562
+ if tokens :
1563
+ lines .append (tokens )
1564
+ tokens = []
1565
+ elif token .type != self .COMMENT_LINE and token .type != self .COMMENT_BLOCK :
1566
+ tokens .append (token )
1567
+ except LexicalError as e :
1568
+ self .Abort ("Found a lexical error in the '%s' script." % name )
1569
+ extends = []
1570
+ functions = {}
1571
+ properties = {}
1572
+ states = {}
1573
+ statements = []
1574
+ try :
1575
+ for line in lines :
1576
+ stat = self .syn .Process (line )
1577
+ if stat :
1578
+ statements .append (stat )
1579
+ except SyntacticError as e :
1580
+ self .Abort ("Found a syntactic error in the '%s' script." % name )
1581
+ header = False
1582
+ if statements [0 ].type == self .STAT_SCRIPTHEADER :
1583
+ if statements [0 ].data .parent :
1584
+ header = True
1585
+ extends = self .GetLineage (statements [0 ].data .parent )
1586
+ parent = self .GetCachedScript (statements [0 ].data .parent )
1587
+ functions .update (parent .functions )
1588
+ properties .update (parent .properties )
1589
+ states .update (parent .states )
1590
+ functions ["GOTOSTATE" ] = Statement (self .STAT_FUNCTIONDEF , 0 , FunctionDef (None , None , False , "GOTOSTATE" , "GoToState" , [ParameterDef (self .KW_STRING , "String" , False , "ASNEWSTATE" , "asNewState" , None )], []))
1591
+ functions ["GETSTATE" ] = Statement (self .STAT_FUNCTIONDEF , 0 , FunctionDef (self .KW_STRING , "String" , False , "GETSTATE" , "GetState" , [], []))
1592
+ functions ["ONINIT" ] = Statement (self .STAT_EVENTDEF , 0 , EventDef (None , "ONINIT" , "OnInit" , [], []))
1593
+ functions ["ONBEGINSTATE" ] = Statement (self .STAT_EVENTDEF , 0 , EventDef (None , "ONBEGINSTATE" , "OnBeginState" , [], []))
1594
+ functions ["ONENDSTATE" ] = Statement (self .STAT_EVENTDEF , 0 , EventDef (None , "ONENDSTATE" , "OnEndState" , [], []))
1595
+ i = 0
1596
+ while i < len (statements ):
1597
+ if statements [i ].type == self .STAT_FUNCTIONDEF or statements [i ].type == self .STAT_EVENTDEF :
1598
+ start = statements [i ]
1599
+ functions [statements [i ].data .name ] = start
1600
+ if not self .KW_NATIVE in statements [i ].data .flags :
1601
+ while i < len (statements ) and not (statements [i ].type == self .STAT_KEYWORD and (statements [i ].data .type == self .KW_ENDFUNCTION or statements [i ].data .type == self .KW_ENDEVENT )):
1602
+ if start .type == self .STAT_FUNCTIONDEF and not start .data .docstring :
1603
+ if statements [i ].type == self .STAT_DOCUMENTATION :
1604
+ start .data .docstring = statements [i ]
1622
1605
i += 1
1623
- elif statements [i ].type == self .STAT_SCRIPTHEADER :
1624
- if not header and statements [i ].data .parent :
1625
- header = True
1626
- extends = self .GetLineage (statements [i ].data .parent )
1627
- i += 1
1606
+ else :
1607
+ if start .type == self .STAT_FUNCTIONDEF and i + 1 < len (statements ) and statements [i + 1 ].type == self .STAT_DOCUMENTATION :
1608
+ start .data .docstring = statements [i + 1 ]
1609
+ i += 1
1610
+ elif statements [i ].type == self .STAT_PROPERTYDEF :
1611
+ start = statements [i ]
1612
+ properties [statements [i ].data .name ] = start
1613
+ if not self .KW_AUTO in statements [i ].data .flags and not self .KW_AUTOREADONLY in statements [i ].data .flags :
1614
+ while i < len (statements ) and not (statements [i ].type == self .STAT_KEYWORD and statements [i ].data .type == self .KW_ENDPROPERTY ):
1615
+ if not start .data .docstring :
1616
+ if statements [i ].type == self .STAT_DOCUMENTATION :
1617
+ start .data .docstring = statements [i ]
1618
+ i += 1
1619
+ else :
1620
+ if i + 1 < len (statements ) and statements [i + 1 ].type == self .STAT_DOCUMENTATION :
1621
+ start .data .docstring = statements [i + 1 ]
1622
+ i += 1
1623
+ elif statements [i ].type == self .STAT_STATEDEF :
1624
+ start = statements [i ]
1625
+ states [statements [i ].data .name ] = start
1626
+ while i < len (statements ) and not (statements [i ].type == self .STAT_KEYWORD and statements [i ].data .type == self .KW_ENDSTATE ):
1627
+ i += 1
1628
+ elif statements [i ].type == self .STAT_SCRIPTHEADER :
1629
+ if not header and statements [i ].data .parent :
1630
+ header = True
1631
+ extends = self .GetLineage (statements [i ].data .parent )
1632
+ i += 1
1628
1633
1629
- self .cache [name ] = CachedScript (extends , properties , functions , states )
1634
+ self .cache [name ] = CachedScript (extends , properties , functions , states )
1630
1635
return True
1631
1636
1632
1637
def GetCachedScript (self , name , line = None ):
0 commit comments