Directives in python

This commit is contained in:
Liam Newman 2018-08-05 11:02:58 -07:00
parent a235009c97
commit c3f7fe75ea
3 changed files with 61 additions and 24 deletions

View File

@ -28,10 +28,12 @@
function Directives(start_block_pattern, end_block_pattern) {
var directives_block_pattern = new RegExp(start_block_pattern.source + / beautify( \w+[:]\w+)+ /.source + end_block_pattern.source, 'g');
start_block_pattern = typeof start_block_pattern === 'string' ? start_block_pattern : start_block_pattern.source;
end_block_pattern = typeof end_block_pattern === 'string' ? end_block_pattern : end_block_pattern.source;
var directives_block_pattern = new RegExp(start_block_pattern + / beautify( \w+[:]\w+)+ /.source + end_block_pattern, 'g');
var directive_pattern = / (\w+)[:](\w+)/g;
var directives_end_ignore_pattern = new RegExp('(?:[\\s\\S]*?)((?:' + start_block_pattern.source + '\\sbeautify\\signore:end\\s' + end_block_pattern.source + ')|$)', 'g');
var directives_end_ignore_pattern = new RegExp('(?:[\\s\\S]*?)((?:' + start_block_pattern + /\sbeautify\signore:end\s/.source + end_block_pattern + ')|$)', 'g');
this.get_directives = function(text) {
if (!text.match(directives_block_pattern)) {

View File

@ -0,0 +1,53 @@
# The MIT License (MIT)
#
# Copyright (c) 2007-2018 Einar Lielmanis, Liam Newman, and contributors.
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation files
# (the "Software"), to deal in the Software without restriction,
# including without limitation the rights to use, copy, modify, merge,
# publish, distribute, sublicense, and/or sell copies of the Software,
# and to permit persons to whom the Software is furnished to do so,
# subject to the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import re
class Directives:
def __init__(self, start_block_pattern, end_block_pattern):
self.__directives_block_pattern = re.compile(start_block_pattern + r' beautify( \w+[:]\w+)+ ' + end_block_pattern)
self.__directive_pattern = re.compile(r' (\w+)[:](\w+)')
self.__directives_end_ignore_pattern = re.compile(r'(?:[\s\S]*?)((?:' + start_block_pattern + r'\sbeautify\signore:end\s' + end_block_pattern + r')|$)')
def get_directives(self, text):
if not self.__directives_block_pattern.match(text):
return None
directives = {}
directive_match = self.__directive_pattern.search(text)
while directive_match:
directives[directive_match.group(1)] = directive_match.group(2)
directive_match = self.__directive_pattern.search(
text, directive_match.end())
return directives
def readIgnored(self, input):
return input.readWhile(self.__directives_end_ignore_pattern)

View File

@ -25,6 +25,7 @@
import re
from ..core.inputscanner import InputScanner
from ..core.token import Token
from ..core.directives import Directives
class TokenTypes:
@ -103,11 +104,7 @@ class Tokenizer:
self.comment_pattern = re.compile(
self.acorn.six.u(r'([^\n\r\u2028\u2029]*)'))
self.directives_block_pattern = re.compile(
r'\/\* beautify( \w+[:]\w+)+ \*\/')
self.directive_pattern = re.compile(r' (\w+)[:](\w+)')
self.directives_end_ignore_pattern = re.compile(
r'([\s\S]*?)((?:\/\*\sbeautify\signore:end\s\*\/)|$)')
self.directives_core = Directives(r'/\*', r'\*/')
self.template_pattern = re.compile(
r'((<\?php|<\?=)[\s\S]*?\?>)|(<%[\s\S]*?%>)')
@ -168,19 +165,6 @@ class Tokenizer:
last = next
return self.tokens
def get_directives(self, text):
if not self.directives_block_pattern.match(text):
return None
directives = {}
directive_match = self.directive_pattern.search(text)
while directive_match:
directives[directive_match.group(1)] = directive_match.group(2)
directive_match = self.directive_pattern.search(
text, directive_match.end())
return directives
def __tokenize_next(self):
self.n_newlines = 0
@ -249,11 +233,9 @@ class Tokenizer:
comment_match = self.input.match(self.block_comment_pattern)
comment = '/*' + comment_match.group(0)
directives = self.get_directives(comment)
directives = self.directives_core.get_directives(comment)
if directives and directives.get('ignore') == 'start':
comment_match = self.input.match(
self.directives_end_ignore_pattern)
comment += comment_match.group(0)
comment += self.directives_core.readIgnored(self.input)
comment = re.sub(self.acorn.allLineBreaks, '\n', comment)
return comment, TOKEN.BLOCK_COMMENT, directives