15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#!/usr/bin/env python
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# Copyright 2007 The Closure Linter Authors. All Rights Reserved.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# Licensed under the Apache License, Version 2.0 (the "License");
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# you may not use this file except in compliance with the License.
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# You may obtain a copy of the License at
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#      http://www.apache.org/licenses/LICENSE-2.0
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# Unless required by applicable law or agreed to in writing, software
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# distributed under the License is distributed on an "AS-IS" BASIS,
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# See the License for the specific language governing permissions and
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# limitations under the License.
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)"""Regular expression based JavaScript matcher classes."""
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)__author__ = ('robbyw@google.com (Robert Walker)',
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)              'ajp@google.com (Andy Perelson)')
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)from closure_linter.common import position
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)from closure_linter.common import tokens
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)# Shorthand
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)Token = tokens.Token
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)Position = position.Position
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Matcher(object):
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  """A token matcher.
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Specifies a pattern to match, the type of token it represents, what mode the
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  token changes to, and what mode the token applies to.
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Modes allow more advanced grammars to be incorporated, and are also necessary
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  to tokenize line by line.  We can have different patterns apply to different
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  modes - i.e. looking for documentation while in comment mode.
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Attributes:
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    regex: The regular expression representing this matcher.
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    type: The type of token indicated by a successful match.
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    result_mode: The mode to move to after a successful match.
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  """
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  def __init__(self, regex, token_type, result_mode=None, line_start=False):
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    """Create a new matcher template.
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    Args:
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      regex: The regular expression to match.
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      token_type: The type of token a successful match indicates.
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      result_mode: What mode to change to after a successful match.  Defaults to
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        None, which means to not change the current mode.
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      line_start: Whether this matcher should only match string at the start
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)        of a line.
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    """
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    self.regex = regex
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    self.type = token_type
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    self.result_mode = result_mode
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    self.line_start = line_start
61