Ejemplo n.º 1
0
 def terminal(self, text):
   """terminal = '"' . (printable - '"') + . '"'
               | "'" . (printable - "'") + . "'" ;"""
   self._attempting(text)
   return alternation([
     concatenation([
       '"',
       one_or_more(
         exclusion(
           self.printable,
           '"'
         ),
         ignore_whitespace=False
       ),
       '"',
     ], ignore_whitespace=False),
     concatenation([
       "'",
       one_or_more(
         exclusion(
           self.printable,
           "'"
         ),
         ignore_whitespace=False
       ),
       "'",
     ], ignore_whitespace=False),
   ])(text).compressed(TokenType.terminal)
Ejemplo n.º 2
0
 def number(self, text):
   """number = ["-"] . ("0" | digit - "0" . {digit}) . ["." . digit +] ;"""
   self._attempting(text)
   return concatenation([
     option(
       "-"
     ),
     alternation([
       "0",
       concatenation([
         exclusion(
           self.digit,
           "0"
         ),
         zero_or_more(
           self.digit,
           ignore_whitespace=False
         ),
       ], ignore_whitespace=False),
     ]),
     option(
       concatenation([
         ".",
         one_or_more(
           self.digit,
           ignore_whitespace=False
         ),
       ], ignore_whitespace=False)
     ),
   ], ignore_whitespace=False)(text).compressed(TokenType.number)
Ejemplo n.º 3
0
  def test_concatenation(self):
    c0 = P.concatenation(["a", "b", "c"], ignore_whitespace=True)
    t0 = "abc"
    n0 = c0(t0)

    self.assertEqual(n0.node_type, P.ParseNodeType.concatenation)
    self.assertFalse(n0.is_value)
    self.assertEqual(n0.position, -len(t0))
    self.assertEqual(len(n0.children), 3)
    self.assertEqual(len(n0), 3)

    with self.assertRaises(P.DeadEnd):
      c0("bcd")

    t1 = """ a\tb\nc"""
    n1 = c0(t1)
    # The position excludes skipped whitespace.
    self.assertEqual(n1.position, -len(t1) + 1)
    self.assertEqual(len(n1), 3)
    self.assertEqual(n1.consumed, 6)

    c1 = P.concatenation(["a", "b", "c"], ignore_whitespace=False)
    n2 = c1(t0)
    self.assertEqual(len(n2.children), 3)

    # c1 raises on t1 because t1 has whitespace but c1 doesn't ignore whitespace.
    with self.assertRaises(P.DeadEnd):
      c1(t1)
Ejemplo n.º 4
0
 def function_call(self, text):
   """function_call = function_name . "(" , [function_args] , ")" ;"""
   self._attempting(text)
   return concatenation([
     self.function_name,
     concatenation([
       "(",
       option(
         self.function_args
       ),
       ")",
     ], ignore_whitespace=True),
   ], ignore_whitespace=False)(text).retyped(TokenType.function_call)
Ejemplo n.º 5
0
 def function_args(self, text):
   """function_args = expression , {"," , function_args} ;"""
   self._attempting(text)
   return concatenation([
     self.expression,
     zero_or_more(
       concatenation([
         ",",
         self.function_args,
       ], ignore_whitespace=True),
       ignore_whitespace=True
     ),
   ], ignore_whitespace=True)(text)
Ejemplo n.º 6
0
 def switch_subject(self, text):
   """switch_subject = "switch" , expression ;"""
   self._attempting(text)
   return concatenation([
     "switch",
     self.expression,
   ], ignore_whitespace=True)(text).retyped(TokenType.switch_subject)
Ejemplo n.º 7
0
 def grouping_group(self, text):
   """grouping_group = "(" , expression , ")" ;"""
   self._attempting(text)
   return concatenation([
     "(",
     self.expression,
     ")",
   ], ignore_whitespace=True)(text).retyped(TokenType.grouping_group)
Ejemplo n.º 8
0
 def subexpression(self, text):
   """subexpression = "(" , expression , ")" ;"""
   self._attempting(text)
   return concatenation([
     "(",
     self.expression,
     ")",
   ], ignore_whitespace=True)(text).retyped(TokenType.subexpression)
Ejemplo n.º 9
0
 def special_handling(self, text):
   """special_handling = "?" , identifier , "?" ;"""
   self._attempting(text)
   return concatenation([
     "?",
     self.identifier,
     "?",
   ], ignore_whitespace=True)(text).retyped(TokenType.special_handling)
Ejemplo n.º 10
0
 def switch_default(self, text):
   """switch_default = "else" , ":" , expression ;"""
   self._attempting(text)
   return concatenation([
     "else",
     ":",
     self.expression,
   ], ignore_whitespace=True)(text).retyped(TokenType.switch_default)
Ejemplo n.º 11
0
 def option_group(self, text):
   """option_group = "[" , expression , "]" ;"""
   self._attempting(text)
   return concatenation([
     "[",
     self.expression,
     "]",
   ], ignore_whitespace=True)(text).retyped(TokenType.option_group)
Ejemplo n.º 12
0
 def repetition_group(self, text):
   """repetition_group = "{" , expression , "}" ;"""
   self._attempting(text)
   return concatenation([
     "{",
     self.expression,
     "}",
   ], ignore_whitespace=True)(text).retyped(TokenType.repetition_group)
Ejemplo n.º 13
0
 def branch_else(self, text):
   """branch_else = "else" , ":" , expression ;"""
   self._attempting(text)
   return concatenation([
     "else",
     ":",
     self.expression,
   ], ignore_whitespace=True)(text).retyped(TokenType.branch_else)
Ejemplo n.º 14
0
 def switch_case(self, text):
   """switch_case = "when" , expression , ":" , expression ;"""
   self._attempting(text)
   return concatenation([
     "when",
     self.expression,
     ":",
     self.expression,
   ], ignore_whitespace=True)(text).retyped(TokenType.switch_case)
Ejemplo n.º 15
0
 def rule(self, text):
   """rule = identifier , "=" , expression , ";" ;"""
   self._attempting(text)
   return concatenation([
     self.identifier,
     "=",
     self.expression,
     ";",
   ], ignore_whitespace=True)(text).retyped(TokenType.rule)
Ejemplo n.º 16
0
 def statement(self, text):
   """statement = comment | assignment , ";" ;"""
   self._attempting(text)
   return alternation([
     self.comment,
     concatenation([
       self.assignment,
       ";",
     ], ignore_whitespace=True),
   ])(text)
Ejemplo n.º 17
0
 def assignment(self, text):
   """assignment = variable , (":=" | "<-") , expression ;"""
   self._attempting(text)
   return concatenation([
     self.variable,
     alternation([
       ":=",
       "<-",
     ]),
     self.expression,
   ], ignore_whitespace=True)(text).retyped(TokenType.assignment)
Ejemplo n.º 18
0
 def expression(self, text):
   """expression = {operator} , expression_terminal , {operator + , expression} ;"""
   self._attempting(text)
   return concatenation([
     zero_or_more(
       self.operator,
       ignore_whitespace=True
     ),
     self.expression_terminal,
     zero_or_more(
       concatenation([
         one_or_more(
           self.operator,
           ignore_whitespace=True
         ),
         self.expression,
       ], ignore_whitespace=True),
       ignore_whitespace=True
     ),
   ], ignore_whitespace=True)(text).retyped(TokenType.expression)
Ejemplo n.º 19
0
 def return_statement(self, text):
   """return_statement = ["return"] , expression , [";"] ;"""
   self._attempting(text)
   return concatenation([
     option(
       "return"
     ),
     self.expression,
     option(
       ";"
     ),
   ], ignore_whitespace=True)(text).retyped(TokenType.return_statement)
Ejemplo n.º 20
0
 def branch(self, text):
   """branch = branch_if , {branch_elif} , branch_else ;
      b"""
   self._attempting(text)
   return concatenation([
     self.branch_if,
     zero_or_more(
       self.branch_elif,
       ignore_whitespace=True
     ),
     self.branch_else,
   ], ignore_whitespace=True)(text).retyped(TokenType.branch)
Ejemplo n.º 21
0
 def string(self, text):
   """string = '"' . {double_string_char} . '"'
             | "'" . {single_string_char} . "'" ;"""
   self._attempting(text)
   return alternation([
     concatenation([
       '"',
       zero_or_more(
         self.double_string_char,
         ignore_whitespace=False
       ),
       '"',
     ], ignore_whitespace=False),
     concatenation([
       "'",
       zero_or_more(
         self.single_string_char,
         ignore_whitespace=False
       ),
       "'",
     ], ignore_whitespace=False),
   ])(text).compressed(TokenType.string)
Ejemplo n.º 22
0
 def eol(self, text):
   """eol = { whitespace - "\n" } . "\n" ;"""
   self._attempting(text)
   return concatenation([
     zero_or_more(
       exclusion(
         self.whitespace,
         "\n"
       ),
       ignore_whitespace=False
     ),
     "\n",
   ], ignore_whitespace=False)(text)
Ejemplo n.º 23
0
 def double_string_char(self, text):
   """double_string_char = "\\" . all_characters - '"'
                         | "\" . '"'
                         | all_characters - '"' ;"""
   self._attempting(text)
   return alternation([
     concatenation([
       "\\\\",
       exclusion(
         self.all_characters,
         '"'
       ),
     ], ignore_whitespace=False),
     concatenation([
       "\\",
       '"',
     ], ignore_whitespace=False),
     exclusion(
       self.all_characters,
       '"'
     ),
   ])(text).retyped(TokenType.double_string_char)
Ejemplo n.º 24
0
 def single_string_char(self, text):
   """single_string_char = "\\" . all_characters - "'"
                         | "\" . "'"
                         | all_characters - "'" ;"""
   self._attempting(text)
   return alternation([
     concatenation([
       "\\\\",
       exclusion(
         self.all_characters,
         "'"
       ),
     ], ignore_whitespace=False),
     concatenation([
       "\\",
       "'",
     ], ignore_whitespace=False),
     exclusion(
       self.all_characters,
       "'"
     ),
   ])(text).retyped(TokenType.single_string_char)
Ejemplo n.º 25
0
 def number(self, text):
   """number = digit - "0" . {digit} ;"""
   self._attempting(text)
   return concatenation([
     exclusion(
       self.digit,
       "0"
     ),
     zero_or_more(
       self.digit,
       ignore_whitespace=False
     ),
   ], ignore_whitespace=False)(text).compressed(TokenType.number)
Ejemplo n.º 26
0
 def complex_identifier(self, text):
   """complex_identifier = "[" . (all_characters - "]") + . "]" ;"""
   self._attempting(text)
   return concatenation([
     "[",
     one_or_more(
       exclusion(
         self.all_characters,
         "]"
       ),
       ignore_whitespace=False
     ),
     "]",
   ], ignore_whitespace=False)(text).retyped(TokenType.complex_identifier)
Ejemplo n.º 27
0
 def comment(self, text):
   """comment = "/*" . {all_characters - "*" | "*" . all_characters - "/"} . "*/" ;"""
   self._attempting(text)
   return concatenation([
     "/*",
     zero_or_more(
       alternation([
         exclusion(
           self.all_characters,
           "*"
         ),
         concatenation([
           "*",
           exclusion(
             self.all_characters,
             "/"
           ),
         ], ignore_whitespace=False),
       ]),
       ignore_whitespace=False
     ),
     "*/",
   ], ignore_whitespace=False)(text).compressed(TokenType.comment)
Ejemplo n.º 28
0
 def directive(self, text):
   """directive = "#" . { all_characters - "\n" } . eol ;"""
   self._attempting(text)
   return concatenation([
     "#",
     zero_or_more(
       exclusion(
         self.all_characters,
         "\n"
       ),
       ignore_whitespace=False
     ),
     self.eol,
   ], ignore_whitespace=False)(text).compressed(TokenType.directive)
Ejemplo n.º 29
0
 def comment(self, text):
   """comment = "(*" . {printable - "*" | "*" . printable - ")"} . "*)" ;"""
   self._attempting(text)
   return concatenation([
     "(*",
     zero_or_more(
       alternation([
         exclusion(
           self.printable,
           "*"
         ),
         concatenation([
           "*",
           exclusion(
             self.printable,
             ")"
           ),
         ], ignore_whitespace=False),
       ]),
       ignore_whitespace=False
     ),
     "*)",
   ], ignore_whitespace=False)(text).compressed(TokenType.comment)
Ejemplo n.º 30
0
 def switch(self, text):
   """switch = switch_subject , switch_case + , [switch_default] ;
      s"""
   self._attempting(text)
   return concatenation([
     self.switch_subject,
     one_or_more(
       self.switch_case,
       ignore_whitespace=True
     ),
     option(
       self.switch_default
     ),
   ], ignore_whitespace=True)(text).retyped(TokenType.switch)