Home | History | Annotate | Download | only in common
      1 #!/usr/bin/env python
      2 #
      3 # Copyright 2007 The Closure Linter Authors. All Rights Reserved.
      4 #
      5 # Licensed under the Apache License, Version 2.0 (the "License");
      6 # you may not use this file except in compliance with the License.
      7 # You may obtain a copy of the License at
      8 #
      9 #      http://www.apache.org/licenses/LICENSE-2.0
     10 #
     11 # Unless required by applicable law or agreed to in writing, software
     12 # distributed under the License is distributed on an "AS-IS" BASIS,
     13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 # See the License for the specific language governing permissions and
     15 # limitations under the License.
     16 
     17 """Regular expression based JavaScript matcher classes."""
     18 
     19 __author__ = ('robbyw (at] google.com (Robert Walker)',
     20               'ajp (at] google.com (Andy Perelson)')
     21 
     22 from closure_linter.common import position
     23 from closure_linter.common import tokens
     24 
     25 # Shorthand
     26 Token = tokens.Token
     27 Position = position.Position
     28 
     29 
     30 class Matcher(object):
     31   """A token matcher.
     32 
     33   Specifies a pattern to match, the type of token it represents, what mode the
     34   token changes to, and what mode the token applies to.
     35 
     36   Modes allow more advanced grammars to be incorporated, and are also necessary
     37   to tokenize line by line.  We can have different patterns apply to different
     38   modes - i.e. looking for documentation while in comment mode.
     39 
     40   Attributes:
     41     regex: The regular expression representing this matcher.
     42     type: The type of token indicated by a successful match.
     43     result_mode: The mode to move to after a successful match.
     44   """
     45 
     46   def __init__(self, regex, token_type, result_mode=None, line_start=False):
     47     """Create a new matcher template.
     48 
     49     Args:
     50       regex: The regular expression to match.
     51       token_type: The type of token a successful match indicates.
     52       result_mode: What mode to change to after a successful match.  Defaults to
     53         None, which means to not change the current mode.
     54       line_start: Whether this matcher should only match string at the start
     55         of a line.
     56     """
     57     self.regex = regex
     58     self.type = token_type
     59     self.result_mode = result_mode
     60     self.line_start = line_start
     61