Coverage for /var/srv/projects/api.amasfac.comuna18.com/tmp/venv/lib/python3.9/site-packages/xlwt/ExcelFormulaLexer.py: 48%

62 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-07-17 14:22 -0600

1from __future__ import print_function 

2# -*- coding: windows-1252 -*- 

3 

4from .antlr import EOF, CommonToken as Tok, TokenStream, TokenStreamException 

5from . import ExcelFormulaParser 

6from re import compile as recompile, IGNORECASE, VERBOSE 

7 

8 

9int_const_pattern = r"\d+\b" 

10flt_const_pattern = r""" 

11 (?: 

12 (?: \d* \. \d+ ) # .1 .12 .123 etc 9.1 etc 98.1 etc 

13 | 

14 (?: \d+ \. ) # 1. 12. 123. etc 

15 ) 

16 # followed by optional exponent part 

17 (?: [Ee] [+-]? \d+ ) ? 

18 """ 

19str_const_pattern = r'"(?:[^"]|"")*"' 

20#range2d_pattern = recompile(r"\$?[A-I]?[A-Z]\$?\d+:\$?[A-I]?[A-Z]\$?\d+" 

21ref2d_r1c1_pattern = r"[Rr]0*[1-9][0-9]*[Cc]0*[1-9][0-9]*" 

22ref2d_pattern = r"\$?[A-I]?[A-Z]\$?0*[1-9][0-9]*" 

23true_pattern = r"TRUE\b" 

24false_pattern = r"FALSE\b" 

25if_pattern = r"IF\b" 

26choose_pattern = r"CHOOSE\b" 

27name_pattern = r"\w[\.\w]*" 

28quotename_pattern = r"'(?:[^']|'')*'" #### It's essential that this bracket be non-grouping. 

29ne_pattern = r"<>" 

30ge_pattern = r">=" 

31le_pattern = r"<=" 

32 

33pattern_type_tuples = ( 

34 (flt_const_pattern, ExcelFormulaParser.NUM_CONST), 

35 (int_const_pattern, ExcelFormulaParser.INT_CONST), 

36 (str_const_pattern, ExcelFormulaParser.STR_CONST), 

37# (range2d_pattern , ExcelFormulaParser.RANGE2D), 

38 (ref2d_r1c1_pattern, ExcelFormulaParser.REF2D_R1C1), 

39 (ref2d_pattern , ExcelFormulaParser.REF2D), 

40 (true_pattern , ExcelFormulaParser.TRUE_CONST), 

41 (false_pattern , ExcelFormulaParser.FALSE_CONST), 

42 (if_pattern , ExcelFormulaParser.FUNC_IF), 

43 (choose_pattern , ExcelFormulaParser.FUNC_CHOOSE), 

44 (name_pattern , ExcelFormulaParser.NAME), 

45 (quotename_pattern, ExcelFormulaParser.QUOTENAME), 

46 (ne_pattern, ExcelFormulaParser.NE), 

47 (ge_pattern, ExcelFormulaParser.GE), 

48 (le_pattern, ExcelFormulaParser.LE), 

49) 

50 

51_re = recompile( 

52 '(' + ')|('.join(i[0] for i in pattern_type_tuples) + ')', 

53 VERBOSE+IGNORECASE) 

54 

55_toktype = [None] + [i[1] for i in pattern_type_tuples] 

56# need dummy at start because re.MatchObject.lastindex counts from 1 

57 

58single_char_lookup = { 

59 '=': ExcelFormulaParser.EQ, 

60 '<': ExcelFormulaParser.LT, 

61 '>': ExcelFormulaParser.GT, 

62 '+': ExcelFormulaParser.ADD, 

63 '-': ExcelFormulaParser.SUB, 

64 '*': ExcelFormulaParser.MUL, 

65 '/': ExcelFormulaParser.DIV, 

66 ':': ExcelFormulaParser.COLON, 

67 ';': ExcelFormulaParser.SEMICOLON, 

68 ',': ExcelFormulaParser.COMMA, 

69 '(': ExcelFormulaParser.LP, 

70 ')': ExcelFormulaParser.RP, 

71 '&': ExcelFormulaParser.CONCAT, 

72 '%': ExcelFormulaParser.PERCENT, 

73 '^': ExcelFormulaParser.POWER, 

74 '!': ExcelFormulaParser.BANG, 

75 } 

76 

77class Lexer(TokenStream): 

78 def __init__(self, text): 

79 self._text = text[:] 

80 self._pos = 0 

81 self._line = 0 

82 

83 def isEOF(self): 

84 return len(self._text) <= self._pos 

85 

86 def curr_ch(self): 

87 return self._text[self._pos] 

88 

89 def next_ch(self, n = 1): 

90 self._pos += n 

91 

92 def is_whitespace(self): 

93 return self.curr_ch() in " \t\n\r\f\v" 

94 

95 def match_pattern(self): 

96 m = _re.match(self._text, self._pos) 

97 if not m: 

98 return None 

99 self._pos = m.end(0) 

100 return Tok(type = _toktype[m.lastindex], text = m.group(0), col = m.start(0) + 1) 

101 

102 def nextToken(self): 

103 # skip whitespace 

104 while not self.isEOF() and self.is_whitespace(): 

105 self.next_ch() 

106 if self.isEOF(): 

107 return Tok(type = EOF) 

108 # first, try to match token with 2 or more chars 

109 t = self.match_pattern() 

110 if t: 

111 return t 

112 # second, we want 1-char tokens 

113 te = self.curr_ch() 

114 try: 

115 ty = single_char_lookup[te] 

116 except KeyError: 

117 raise TokenStreamException( 

118 "Unexpected char %r in column %u." % (self.curr_ch(), self._pos)) 

119 self.next_ch() 

120 return Tok(type=ty, text=te, col=self._pos) 

121 

122if __name__ == '__main__': 122 ↛ 123line 122 didn't jump to line 123, because the condition on line 122 was never true

123 try: 

124 for t in Lexer(""" 1.23 456 "abcd" R2C2 a1 iv65536 true false if choose a_name 'qname' <> >= <= """): 

125 print(t) 

126 except TokenStreamException as e: 

127 print("error:", e)