First baby steps towards dual Python2+3 compatibility

This commit is contained in:
Sei Lisa 2019-01-15 20:27:02 +01:00
parent 789eb85bfe
commit fe2dd9a721
17 changed files with 319 additions and 175 deletions

View file

@ -34,12 +34,13 @@
# The JSON functions have been separated to their own module.
import re
from lslcommon import *
import lslcommon
from lslopt.lslcommon import *
from lslopt import lslcommon
from ctypes import c_float
import math
import hashlib
from base64 import b64encode, b64decode
from strutil import *
# Regular expressions used along the code. They are needed mainly because
@ -58,18 +59,49 @@ from base64 import b64encode, b64decode
# as is (vector)"<1,inf,info>". The 1st gives <0,0,0>, the others <1,inf,inf>.
# The lookahead (?!i) is essential for parsing them that way without extra code.
# Note that '|' in REs is order-sensitive.
float_re = re.compile(ur'^\s*[+-]?(?:0(x)(?:[0-9a-f]+(?:\.[0-9a-f]*)?|\.[0-9a-f]+)(?:p[+-]?[0-9]+)?'
ur'|(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)(?:e[+-]?[0-9]+)?|inf|(nan))',
re.I)
vfloat_re = re.compile(ur'^\s*[+-]?(?:0(x)(?:[0-9a-f]+(?:\.[0-9a-f]*)?|\.[0-9a-f]+)(?:p[+-]?[0-9]+)?'
ur'|(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)(?:e[+-]?[0-9]+)?|infinity|inf(?!i)|(nan))',
re.I)
float_re = re.compile(str2u(r'''
^\s*[+-]?(?:
0(x)(?: # Hex float or hex int (captures the 'x')
[0-9a-f]+(?:\.[0-9a-f]*)?
|\.[0-9a-f]+ # Hex digits
)(?:
p[+-]?[0-9]+ # Hex float exponent
)? # (optional)
|(?: # Decimal float or decimal int
[0-9]+(?:\.[0-9]*)?
|\.[0-9]+ # Decimal digits
)(?:
e[+-]?[0-9]+ # Decimal float exponent
)? # (optional)
|inf # Infinity
|(nan) # NaN (captured)
)
'''), re.I | re.X)
vfloat_re = re.compile(str2u(r'''
^\s*[+-]?(?:
0(x)(?: # Hex float or hex int (captures the 'x')
[0-9a-f]+(?:\.[0-9a-f]*)?
|\.[0-9a-f]+ # Hex digits
)(?:
p[+-]?[0-9]+ # Hex float exponent
)? # (optional)
|(?: # Decimal float or decimal int
[0-9]+(?:\.[0-9]*)?
|\.[0-9]+ # Decimal digits
)(?:
e[+-]?[0-9]+ # Decimal float exponent
)? # (optional)
|infinity|inf(?!i) # Infinity (the only difference with the above)
|(nan) # NaN (captured)
)
'''), re.I | re.X)
int_re = re.compile(ur'^0(x)[0-9a-f]+|^\s*[+-]?[0-9]+', re.I)
int_re = re.compile(str2u(r'^0(x)[0-9a-f]+|^\s*[+-]?[0-9]+'), re.I)
key_re = re.compile(ur'^[0-9a-f]{8}(?:-[0-9a-f]{4}){4}[0-9a-f]{8}$', re.I)
key_re = re.compile(str2u(r'^[0-9a-f]{8}(?:-[0-9a-f]{4}){4}[0-9a-f]{8}$'),
re.I)
b64_re = re.compile(ur'^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2,3})?')
b64_re = re.compile(str2u(r'^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2,3})?'))
ZERO_VECTOR = Vector((0.0, 0.0, 0.0))
ZERO_ROTATION = Quaternion((0.0, 0.0, 0.0, 1.0))

View file

@ -18,6 +18,7 @@
# Classes, functions and variables for use of all modules.
import sys
from strutil import *
_exclusions = frozenset(('nt','t','name','value','ch', 'X','SEF'))

View file

@ -17,8 +17,8 @@
# Dead Code Removal optimization
import lslfuncs
from lslcommon import nr
from lslopt import lslfuncs
from lslopt.lslcommon import nr
class deadcode(object):

View file

@ -17,8 +17,8 @@
# Extra functions that have predictable return values for certain arguments.
from lslcommon import Key, Vector #, Quaternion
from lslbasefuncs import ELSLCantCompute, fi,ff,fs,fk,v2f,q2f,fl, \
from lslopt.lslcommon import Key, Vector #, Quaternion
from lslopt.lslbasefuncs import ELSLCantCompute, fi,ff,fs,fk,v2f,q2f,fl, \
NULL_KEY, ZERO_VECTOR, ZERO_ROTATION, \
TOUCH_INVALID_TEXCOORD, cond
ff, q2f # keep pyflakes happy as these are not used

View file

@ -17,12 +17,12 @@
# Constant folding and simplification of expressions and statements.
import lslcommon
from lslcommon import Vector, Quaternion, warning, nr
import lslfuncs
from lslfuncs import ZERO_VECTOR, ZERO_ROTATION
from lslopt import lslcommon
from lslopt.lslcommon import Vector, Quaternion, warning, nr
from lslopt import lslfuncs
from lslopt.lslfuncs import ZERO_VECTOR, ZERO_ROTATION
import math
from lslfuncopt import OptimizeFunc, OptimizeArgs, FuncOptSetup
from lslopt.lslfuncopt import OptimizeFunc, OptimizeArgs, FuncOptSetup
# TODO: Remove special handling of @ within IF,WHILE,FOR,DO

View file

@ -18,9 +18,9 @@
# Optimize calls to LSL library functions and parameters where possible
# This is dependent on the LSL function library.
import lslcommon
from lslcommon import Key, Vector, Quaternion, nr
import lslfuncs
from lslopt import lslcommon
from lslopt.lslcommon import Key, Vector, Quaternion, nr
from lslopt import lslfuncs
def OptimizeArgs(node, sym):
"""Transform function arguments to shorter equivalents where possible."""

View file

@ -17,6 +17,6 @@
# Put all LSL functions together in one single module
from lslbasefuncs import *
from lsljson import *
from lslextrafuncs import *
from lslopt.lslbasefuncs import *
from lslopt.lsljson import *
from lslopt.lslextrafuncs import *

View file

@ -19,8 +19,8 @@
import re
import math
from lslcommon import *
from lslbasefuncs import llStringTrim, fs, fl, InternalTypecast
from lslopt.lslcommon import *
from lslopt.lslbasefuncs import llStringTrim, fs, fl, InternalTypecast
# INCOMPATIBILITY NOTE: The JSON functions in SL have very weird behaviour
# in corner cases. Despite our best efforts, that behaviour is not replicated
@ -44,8 +44,8 @@ JSON_DELETE = u'\uFDD8'
JSON_APPEND = -1
jsonesc_re = re.compile(u'[\x08\x09\x0A\x0C\x0D"/\\\\]')
jsonesc_dict = {u'\x08':ur'\b', u'\x09':ur'\t', u'\x0A':ur'\n', u'\x0C':ur'\f',
u'\x0D':ur'\r', u'"':ur'\"', u'/':ur'\/', u'\\':ur'\\'}
jsonesc_dict = {u'\x08':u'\\b', u'\x09':u'\\t', u'\x0A':u'\\n', u'\x0C':u'\\f',
u'\x0D':u'\\r', u'"':u'\\"', u'/':u'\\/', u'\\':u'\\\\'}
jsonunesc_dict = {u'b':u'\x08', u't':u'\x09', u'n':u'\x0A', u'f':u'\x0C', u'r':u'\x0D'}
# LSL JSON numbers differ from standard JSON numbers in many respects:
@ -72,18 +72,37 @@ jsonunesc_dict = {u'b':u'\x08', u't':u'\x09', u'n':u'\x0A', u'f':u'\x0C', u'r':u
# elements when appropriate.
# Real JSON number parser:
#jsonnum_re = re.compile(ur'-?(?:[1-9][0-9]*|0)(?:\.[0-9]+)?(?:[Ee][+-]?[0-9]+)?')
#jsonnum_re = re.compile(str2u(
# r'-?(?:[1-9][0-9]*|0)(?:\.[0-9]+)?(?:[Ee][+-]?[0-9]+)?'
# ))
# BUG-6466 active:
jsonnumbug_re = re.compile(ur'-?(?:[0-9]*([Ee])-?[0-9]*\.?[0-9]*|(?=[0-9Ee.])[0-9]*(\.?[0-9]*(?:[Ee]-?)?[0-9]*))')
jsonnumbug_re = re.compile(str2u(r'''
-?(?:
[0-9]*([Ee])-?[0-9]*\.?[0-9]*
|(?=[0-9Ee.])[0-9]*(\.?[0-9]*(?:[Ee]-?)?[0-9]*)
)
'''), re.X)
# BUG-6466 fixed:
# The new RE is just a modified version of the crap, allowing + exponents and
# disallowing zeros, sometimes even when legal (e.g. 0e0)
#jsonnum_re = re.compile(ur'-?(?:(?=[1-9]|\.(?:[^e]|$)|0(?:[^0-9e]|$))[0-9]*([Ee])[+-]?[0-9]*\.?[0-9]*|(?=[1-9]|\.(?:[^e]|$)|0(?:[^0-9e]|$))[0-9]*(\.?[0-9]*(?:[Ee][+-]?)?[0-9]*))')
#jsonnum_re = re.compile(str2u(r'''
# -?(?:
# (?=[1-9]|\.(?:[^e]|$)
# |0(?:[^0-9e]|$))[0-9]*([Ee])[+-]?[0-9]*\.?[0-9]*
# |(?=[1-9]|\.(?:[^e]|$)
# |0(?:[^0-9e]|$))[0-9]*(\.?[0-9]*(?:[Ee][+-]?)?[0-9]*)
# )
# '''), re.X)
# They've fixed BUG-6657 by bringing BUG-6466 back to life.
jsonnum_re = re.compile(ur'-?(?:[0-9]*([Ee])-?[0-9]*\.?[0-9]*|(?=[0-9Ee.])[0-9]*(\.?[0-9]*(?:[Ee]-?)?[0-9]*))')
jsonnum_re = re.compile(str2u(r'''
-?(?:
[0-9]*([Ee])-?[0-9]*\.?[0-9]*
|(?=[0-9Ee.])[0-9]*(\.?[0-9]*(?:[Ee]-?)?[0-9]*)
)
'''), re.X)
jsonstring_re = re.compile(ur'"(?:[^"\\]|\\.)*"')
jsonstring_re = re.compile(str2u(r'"(?:[^"\\]|\\.)*"'))
# This might need some explanation. The ] and - are included in the first
# set, the ] in the first after the ^ and the - in the last positions of
@ -91,7 +110,7 @@ jsonstring_re = re.compile(ur'"(?:[^"\\]|\\.)*"')
# though it confuses things. The set comprises any character not in
# -{}[],:"0123456789
# The second set comprises zero or more characters not in ,:]}
#word_re = re.compile(ur'[^][{}0-9",:-][^]},:]*')
#word_re = re.compile(str2u(r'[^][{}0-9",:-][^]},:]*'))
# Screw that, we're using just a fallback.
jsoncatchall_re = re.compile(u'(.*?)[\x09\x0A\x0B\x0C\x0D ]*(?:[]},]|$)')

View file

@ -17,8 +17,8 @@
# Optimizations that have a negative effect on other stages.
import lslcommon
from lslcommon import nr
from lslopt import lslcommon
from lslopt.lslcommon import nr
#from lslcommon import Vector, Quaternion
#import lslfuncs
#from lslfuncs import ZERO_VECTOR, ZERO_ROTATION

View file

@ -18,8 +18,8 @@
# Load the builtins and function properties.
import sys, re
from lslcommon import types, warning, Vector, Quaternion
import lslcommon, lslfuncs
from lslopt.lslcommon import types, warning, Vector, Quaternion
from lslopt import lslcommon, lslfuncs
def LoadLibrary(builtins = None, fndata = None):
"""Load builtins.txt and fndata.txt (or the given filenames) and return
@ -40,21 +40,21 @@ def LoadLibrary(builtins = None, fndata = None):
# Library read code
parse_lin_re = re.compile(
r'^\s*([a-z]+)\s+'
r'([a-zA-Z_][a-zA-Z0-9_]*)\s*\(\s*('
r'[a-z]+\s+[a-zA-Z_][a-zA-Z0-9_]*'
r'(?:\s*,\s*[a-z]+\s+[a-zA-Z_][a-zA-Z0-9_]*)*'
r')?\s*\)\s*$'
r'|'
r'^\s*const\s+([a-z]+)'
r'\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*=\s*(.*?)\s*$'
r'|'
r'^\s*(?:#.*|//.*)?$')
parse_arg_re = re.compile(r'^\s*([a-z]+)\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*$')
parse_fp_re = re.compile(r'^\s*(-?(?=[0-9]|\.[0-9])[0-9]*'
r'((?:\.[0-9]*)?(?:[Ee][+-]?[0-9]+)?))\s*$')
parse_int_re = re.compile(r'^\s*(-?0x[0-9A-Fa-f]+|-?[0-9]+)\s*$')
parse_str_re = re.compile(ur'^"((?:[^"\\]|\\.)*)"$')
br'^\s*([a-z]+)\s+'
br'([a-zA-Z_][a-zA-Z0-9_]*)\s*\(\s*('
br'[a-z]+\s+[a-zA-Z_][a-zA-Z0-9_]*'
br'(?:\s*,\s*[a-z]+\s+[a-zA-Z_][a-zA-Z0-9_]*)*'
br')?\s*\)\s*$'
br'|'
br'^\s*const\s+([a-z]+)'
br'\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*=\s*(.*?)\s*$'
br'|'
br'^\s*(?:#.*|//.*)?$')
parse_arg_re = re.compile(br'^\s*([a-z]+)\s+([a-zA-Z_][a-zA-Z0-9_]*)\s*$')
parse_fp_re = re.compile(br'^\s*(-?(?=[0-9]|\.[0-9])[0-9]*'
br'((?:\.[0-9]*)?(?:[Ee][+-]?[0-9]+)?))\s*$')
parse_int_re = re.compile(br'^\s*(-?0x[0-9A-Fa-f]+|-?[0-9]+)\s*$')
parse_str_re = re.compile(u'^"((?:[^"\\\\]|\\\\.)*)"$')
f = open(builtins, 'rb')
try:

View file

@ -17,13 +17,13 @@
# Optimizer class that wraps and calls the other parts.
import lslfuncs
from lslopt import lslfuncs
from lslcommon import nr
from lslfoldconst import foldconst
from lslrenamer import renamer
from lsldeadcode import deadcode
from lsllastpass import lastpass
from lslopt.lslcommon import nr
from lslopt.lslfoldconst import foldconst
from lslopt.lslrenamer import renamer
from lslopt.lsldeadcode import deadcode
from lslopt.lsllastpass import lastpass
class optimizer(foldconst, renamer, deadcode, lastpass):

View file

@ -17,9 +17,9 @@
# Convert an abstract syntax tree + symbol table back to a script as text.
import lslfuncs
import lslcommon
from lslcommon import Key, Vector, Quaternion, warning
from lslopt import lslfuncs
from lslopt import lslcommon
from lslopt.lslcommon import Key, Vector, Quaternion, warning
from math import copysign
debugScopes = False

View file

@ -20,8 +20,9 @@
# TODO: Add info to be able to propagate error position to the source.
from lslcommon import Key, Vector, Quaternion, types, nr
import lslcommon, lslfuncs
from lslopt.lslcommon import Key, Vector, Quaternion, types, nr
from lslopt import lslcommon, lslfuncs
from strutil import *
import re
# Note this module was basically written from bottom to top, which may help
@ -70,8 +71,8 @@ class EParse(Exception):
self.errorpos = parser.errorpos
self.lno, self.cno, self.fname = GetErrLineCol(parser)
filename = (self.fname.decode('utf8', 'replace')
.replace(u'\\', ur'\\')
.replace(u'"', ur'\"')
.replace(u'\\', u'\\\\')
.replace(u'"', u'\\"')
)
if parser.processpre and filename != '<stdin>':