['\"])\s*\d{2}(\d{2})?.?\d{2}.?\d{2}\s*(?P=quote)\s*\}", Literal.Date), # Time literals (r"\{\s*t\s*(?P['\"])\s*(?:\d+\s+)?\d{1,2}.?\d{1,2}.?\d{1,2}(\.\d*)?\s*(?P=quote)\s*\}", Literal.Date), # Timestamp literals ( r"\{\s*ts\s*(?P['\"])\s*" r"\d{2}(?:\d{2})?.?\d{2}.?\d{2}" # Date part r"\s+" # Whitespace between date and time r"\d{1,2}.?\d{1,2}.?\d{1,2}(\.\d*)?" # Time part r"\s*(?P=quote)\s*\}", Literal.Date ), # String literals (r"'", String.Single, 'single-quoted-string'), (r'"', String.Double, 'double-quoted-string'), # Variables (r'@@(?:global\.|persist\.|persist_only\.|session\.)?[a-z_]+', Name.Variable), (r'@[a-z0-9_$.]+', Name.Variable), (r"@'", Name.Variable, 'single-quoted-variable'), (r'@"', Name.Variable, 'double-quoted-variable'), (r"@`", Name.Variable, 'backtick-quoted-variable'), (r'\?', Name.Variable), # For demonstrating prepared statements # Operators (r'[!%&*+/:<=>^|~-]+', Operator), # Exceptions; these words tokenize differently in different contexts. (r'\b(set)(?!\s*\()', Keyword), (r'\b(character)(\s+)(set)\b', bygroups(Keyword, Whitespace, Keyword)), # In all other known cases, "SET" is tokenized by MYSQL_DATATYPES. (words(MYSQL_CONSTANTS, prefix=r'\b', suffix=r'\b'), Name.Constant), (words(MYSQL_DATATYPES, prefix=r'\b', suffix=r'\b'), Keyword.Type), (words(MYSQL_KEYWORDS, prefix=r'\b', suffix=r'\b'), Keyword), (words(MYSQL_FUNCTIONS, prefix=r'\b', suffix=r'\b(\s*)(\()'), bygroups(Name.Function, Whitespace, Punctuation)), # Schema object names # # Note: Although the first regex supports unquoted all-numeric # identifiers, this will not be a problem in practice because # numeric literals have already been handled above. # ('[0-9a-z$_\u0080-\uffff]+', Name), (r'`', Name.Quoted, 'schema-object-name'), # Punctuation (r'[(),.;]', Punctuation), ], # Multiline comment substates # --------------------------- 'optimizer-hints': [ (r'[^*a-z]+', Comment.Special), (r'\*/', Comment.Special, '#pop'), (words(MYSQL_OPTIMIZER_HINTS, suffix=r'\b'), Comment.Preproc), ('[a-z]+', Comment.Special), (r'\*', Comment.Special), ], 'multiline-comment': [ (r'[^*]+', Comment.Multiline), (r'\*/', Comment.Multiline, '#pop'), (r'\*', Comment.Multiline), ], # String substates # ---------------- 'single-quoted-string': [ (r"[^'\\]+", String.Single), (r"''", String.Escape), (r"""\\[0'"bnrtZ\\%_]""", String.Escape), (r"'", String.Single, '#pop'), ], 'double-quoted-string': [ (r'[^"\\]+', String.Double), (r'""', String.Escape), (r"""\\[0'"bnrtZ\\%_]""", String.Escape), (r'"', String.Double, '#pop'), ], # Variable substates # ------------------ 'single-quoted-variable': [ (r"[^']+", Name.Variable), (r"''", Name.Variable), (r"'", Name.Variable, '#pop'), ], 'double-quoted-variable': [ (r'[^"]+', Name.Variable), (r'""', Name.Variable), (r'"', Name.Variable, '#pop'), ], 'backtick-quoted-variable': [ (r'[^`]+', Name.Variable), (r'``', Name.Variable), (r'`', Name.Variable, '#pop'), ], # Schema object name substates # ---------------------------- # # "Name.Quoted" and "Name.Quoted.Escape" are non-standard but # formatters will style them as "Name" by default but add # additional styles based on the token name. This gives users # flexibility to add custom styles as desired. # 'schema-object-name': [ (r'[^`]+', Name.Quoted), (r'``', Name.Quoted.Escape), (r'`', Name.Quoted, '#pop'), ], } def analyse_text(text): rating = 0 name_between_backtick_count = len( name_between_backtick_re.findall(text)) name_between_bracket_count = len( name_between_bracket_re.findall(text)) # Same logic as above in the TSQL analysis dialect_name_count = name_between_backtick_count + name_between_bracket_count if dialect_name_count >= 1 and \ name_between_backtick_count >= 2 * name_between_bracket_count: # Found at least twice as many `name` as [name]. rating += 0.5 elif name_between_backtick_count > name_between_bracket_count: rating += 0.2 elif name_between_backtick_count > 0: rating += 0.1 return rating class SqliteConsoleLexer(Lexer): """ Lexer for example sessions using sqlite3. """ name = 'sqlite3con' aliases = ['sqlite3'] filenames = ['*.sqlite3-console'] mimetypes = ['text/x-sqlite3-console'] url = 'https://www.sqlite.org' version_added = '0.11' def get_tokens_unprocessed(self, data): sql = SqlLexer(**self.options) curcode = '' insertions = [] for match in line_re.finditer(data): line = match.group() prompt_match = sqlite_prompt_re.match(line) if prompt_match is not None: insertions.append((len(curcode), [(0, Generic.Prompt, line[:7])])) insertions.append((len(curcode), [(7, Whitespace, ' ')])) curcode += line[8:] else: if curcode: yield from do_insertions(insertions, sql.get_tokens_unprocessed(curcode)) curcode = '' insertions = [] if line.startswith('SQL error: '): yield (match.start(), Generic.Traceback, line) else: yield (match.start(), Generic.Output, line) if curcode: yield from do_insertions(insertions, sql.get_tokens_unprocessed(curcode)) class RqlLexer(RegexLexer): """ Lexer for Relation Query Language. """ name = 'RQL' url = 'http://www.logilab.org/project/rql' aliases = ['rql'] filenames = ['*.rql'] mimetypes = ['text/x-rql'] version_added = '2.0' flags = re.IGNORECASE tokens = { 'root': [ (r'\s+', Whitespace), (r'(DELETE|SET|INSERT|UNION|DISTINCT|WITH|WHERE|BEING|OR' r'|AND|NOT|GROUPBY|HAVING|ORDERBY|ASC|DESC|LIMIT|OFFSET' r'|TODAY|NOW|TRUE|FALSE|NULL|EXISTS)\b', Keyword), (r'[+*/<>=%-]', Operator), (r'(Any|is|instance_of|CWEType|CWRelation)\b', Name.Builtin), (r'[0-9]+', Number.Integer), (r'[A-Z_]\w*\??', Name), (r"'(''|[^'])*'", String.Single), (r'"(""|[^"])*"', String.Single), (r'[;:()\[\],.]', Punctuation) ], }
['\"])\s*(?:\d+\s+)?\d{1,2}.?\d{1,2}.?\d{1,2}(\.\d*)?\s*(?P=quote)\s*\}", Literal.Date), # Timestamp literals ( r"\{\s*ts\s*(?P['\"])\s*" r"\d{2}(?:\d{2})?.?\d{2}.?\d{2}" # Date part r"\s+" # Whitespace between date and time r"\d{1,2}.?\d{1,2}.?\d{1,2}(\.\d*)?" # Time part r"\s*(?P=quote)\s*\}", Literal.Date ), # String literals (r"'", String.Single, 'single-quoted-string'), (r'"', String.Double, 'double-quoted-string'), # Variables (r'@@(?:global\.|persist\.|persist_only\.|session\.)?[a-z_]+', Name.Variable), (r'@[a-z0-9_$.]+', Name.Variable), (r"@'", Name.Variable, 'single-quoted-variable'), (r'@"', Name.Variable, 'double-quoted-variable'), (r"@`", Name.Variable, 'backtick-quoted-variable'), (r'\?', Name.Variable), # For demonstrating prepared statements # Operators (r'[!%&*+/:<=>^|~-]+', Operator), # Exceptions; these words tokenize differently in different contexts. (r'\b(set)(?!\s*\()', Keyword), (r'\b(character)(\s+)(set)\b', bygroups(Keyword, Whitespace, Keyword)), # In all other known cases, "SET" is tokenized by MYSQL_DATATYPES. (words(MYSQL_CONSTANTS, prefix=r'\b', suffix=r'\b'), Name.Constant), (words(MYSQL_DATATYPES, prefix=r'\b', suffix=r'\b'), Keyword.Type), (words(MYSQL_KEYWORDS, prefix=r'\b', suffix=r'\b'), Keyword), (words(MYSQL_FUNCTIONS, prefix=r'\b', suffix=r'\b(\s*)(\()'), bygroups(Name.Function, Whitespace, Punctuation)), # Schema object names # # Note: Although the first regex supports unquoted all-numeric # identifiers, this will not be a problem in practice because # numeric literals have already been handled above. # ('[0-9a-z$_\u0080-\uffff]+', Name), (r'`', Name.Quoted, 'schema-object-name'), # Punctuation (r'[(),.;]', Punctuation), ], # Multiline comment substates # --------------------------- 'optimizer-hints': [ (r'[^*a-z]+', Comment.Special), (r'\*/', Comment.Special, '#pop'), (words(MYSQL_OPTIMIZER_HINTS, suffix=r'\b'), Comment.Preproc), ('[a-z]+', Comment.Special), (r'\*', Comment.Special), ], 'multiline-comment': [ (r'[^*]+', Comment.Multiline), (r'\*/', Comment.Multiline, '#pop'), (r'\*', Comment.Multiline), ], # String substates # ---------------- 'single-quoted-string': [ (r"[^'\\]+", String.Single), (r"''", String.Escape), (r"""\\[0'"bnrtZ\\%_]""", String.Escape), (r"'", String.Single, '#pop'), ], 'double-quoted-string': [ (r'[^"\\]+', String.Double), (r'""', String.Escape), (r"""\\[0'"bnrtZ\\%_]""", String.Escape), (r'"', String.Double, '#pop'), ], # Variable substates # ------------------ 'single-quoted-variable': [ (r"[^']+", Name.Variable), (r"''", Name.Variable), (r"'", Name.Variable, '#pop'), ], 'double-quoted-variable': [ (r'[^"]+', Name.Variable), (r'""', Name.Variable), (r'"', Name.Variable, '#pop'), ], 'backtick-quoted-variable': [ (r'[^`]+', Name.Variable), (r'``', Name.Variable), (r'`', Name.Variable, '#pop'), ], # Schema object name substates # ---------------------------- # # "Name.Quoted" and "Name.Quoted.Escape" are non-standard but # formatters will style them as "Name" by default but add # additional styles based on the token name. This gives users # flexibility to add custom styles as desired. # 'schema-object-name': [ (r'[^`]+', Name.Quoted), (r'``', Name.Quoted.Escape), (r'`', Name.Quoted, '#pop'), ], } def analyse_text(text): rating = 0 name_between_backtick_count = len( name_between_backtick_re.findall(text)) name_between_bracket_count = len( name_between_bracket_re.findall(text)) # Same logic as above in the TSQL analysis dialect_name_count = name_between_backtick_count + name_between_bracket_count if dialect_name_count >= 1 and \ name_between_backtick_count >= 2 * name_between_bracket_count: # Found at least twice as many `name` as [name]. rating += 0.5 elif name_between_backtick_count > name_between_bracket_count: rating += 0.2 elif name_between_backtick_count > 0: rating += 0.1 return rating class SqliteConsoleLexer(Lexer): """ Lexer for example sessions using sqlite3. """ name = 'sqlite3con' aliases = ['sqlite3'] filenames = ['*.sqlite3-console'] mimetypes = ['text/x-sqlite3-console'] url = 'https://www.sqlite.org' version_added = '0.11' def get_tokens_unprocessed(self, data): sql = SqlLexer(**self.options) curcode = '' insertions = [] for match in line_re.finditer(data): line = match.group() prompt_match = sqlite_prompt_re.match(line) if prompt_match is not None: insertions.append((len(curcode), [(0, Generic.Prompt, line[:7])])) insertions.append((len(curcode), [(7, Whitespace, ' ')])) curcode += line[8:] else: if curcode: yield from do_insertions(insertions, sql.get_tokens_unprocessed(curcode)) curcode = '' insertions = [] if line.startswith('SQL error: '): yield (match.start(), Generic.Traceback, line) else: yield (match.start(), Generic.Output, line) if curcode: yield from do_insertions(insertions, sql.get_tokens_unprocessed(curcode)) class RqlLexer(RegexLexer): """ Lexer for Relation Query Language. """ name = 'RQL' url = 'http://www.logilab.org/project/rql' aliases = ['rql'] filenames = ['*.rql'] mimetypes = ['text/x-rql'] version_added = '2.0' flags = re.IGNORECASE tokens = { 'root': [ (r'\s+', Whitespace), (r'(DELETE|SET|INSERT|UNION|DISTINCT|WITH|WHERE|BEING|OR' r'|AND|NOT|GROUPBY|HAVING|ORDERBY|ASC|DESC|LIMIT|OFFSET' r'|TODAY|NOW|TRUE|FALSE|NULL|EXISTS)\b', Keyword), (r'[+*/<>=%-]', Operator), (r'(Any|is|instance_of|CWEType|CWRelation)\b', Name.Builtin), (r'[0-9]+', Number.Integer), (r'[A-Z_]\w*\??', Name), (r"'(''|[^'])*'", String.Single), (r'"(""|[^"])*"', String.Single), (r'[;:()\[\],.]', Punctuation) ], }
['\"])\s*" r"\d{2}(?:\d{2})?.?\d{2}.?\d{2}" # Date part r"\s+" # Whitespace between date and time r"\d{1,2}.?\d{1,2}.?\d{1,2}(\.\d*)?" # Time part r"\s*(?P=quote)\s*\}", Literal.Date ), # String literals (r"'", String.Single, 'single-quoted-string'), (r'"', String.Double, 'double-quoted-string'), # Variables (r'@@(?:global\.|persist\.|persist_only\.|session\.)?[a-z_]+', Name.Variable), (r'@[a-z0-9_$.]+', Name.Variable), (r"@'", Name.Variable, 'single-quoted-variable'), (r'@"', Name.Variable, 'double-quoted-variable'), (r"@`", Name.Variable, 'backtick-quoted-variable'), (r'\?', Name.Variable), # For demonstrating prepared statements # Operators (r'[!%&*+/:<=>^|~-]+', Operator), # Exceptions; these words tokenize differently in different contexts. (r'\b(set)(?!\s*\()', Keyword), (r'\b(character)(\s+)(set)\b', bygroups(Keyword, Whitespace, Keyword)), # In all other known cases, "SET" is tokenized by MYSQL_DATATYPES. (words(MYSQL_CONSTANTS, prefix=r'\b', suffix=r'\b'), Name.Constant), (words(MYSQL_DATATYPES, prefix=r'\b', suffix=r'\b'), Keyword.Type), (words(MYSQL_KEYWORDS, prefix=r'\b', suffix=r'\b'), Keyword), (words(MYSQL_FUNCTIONS, prefix=r'\b', suffix=r'\b(\s*)(\()'), bygroups(Name.Function, Whitespace, Punctuation)), # Schema object names # # Note: Although the first regex supports unquoted all-numeric # identifiers, this will not be a problem in practice because # numeric literals have already been handled above. # ('[0-9a-z$_\u0080-\uffff]+', Name), (r'`', Name.Quoted, 'schema-object-name'), # Punctuation (r'[(),.;]', Punctuation), ], # Multiline comment substates # --------------------------- 'optimizer-hints': [ (r'[^*a-z]+', Comment.Special), (r'\*/', Comment.Special, '#pop'), (words(MYSQL_OPTIMIZER_HINTS, suffix=r'\b'), Comment.Preproc), ('[a-z]+', Comment.Special), (r'\*', Comment.Special), ], 'multiline-comment': [ (r'[^*]+', Comment.Multiline), (r'\*/', Comment.Multiline, '#pop'), (r'\*', Comment.Multiline), ], # String substates # ---------------- 'single-quoted-string': [ (r"[^'\\]+", String.Single), (r"''", String.Escape), (r"""\\[0'"bnrtZ\\%_]""", String.Escape), (r"'", String.Single, '#pop'), ], 'double-quoted-string': [ (r'[^"\\]+', String.Double), (r'""', String.Escape), (r"""\\[0'"bnrtZ\\%_]""", String.Escape), (r'"', String.Double, '#pop'), ], # Variable substates # ------------------ 'single-quoted-variable': [ (r"[^']+", Name.Variable), (r"''", Name.Variable), (r"'", Name.Variable, '#pop'), ], 'double-quoted-variable': [ (r'[^"]+', Name.Variable), (r'""', Name.Variable), (r'"', Name.Variable, '#pop'), ], 'backtick-quoted-variable': [ (r'[^`]+', Name.Variable), (r'``', Name.Variable), (r'`', Name.Variable, '#pop'), ], # Schema object name substates # ---------------------------- # # "Name.Quoted" and "Name.Quoted.Escape" are non-standard but # formatters will style them as "Name" by default but add # additional styles based on the token name. This gives users # flexibility to add custom styles as desired. # 'schema-object-name': [ (r'[^`]+', Name.Quoted), (r'``', Name.Quoted.Escape), (r'`', Name.Quoted, '#pop'), ], } def analyse_text(text): rating = 0 name_between_backtick_count = len( name_between_backtick_re.findall(text)) name_between_bracket_count = len( name_between_bracket_re.findall(text)) # Same logic as above in the TSQL analysis dialect_name_count = name_between_backtick_count + name_between_bracket_count if dialect_name_count >= 1 and \ name_between_backtick_count >= 2 * name_between_bracket_count: # Found at least twice as many `name` as [name]. rating += 0.5 elif name_between_backtick_count > name_between_bracket_count: rating += 0.2 elif name_between_backtick_count > 0: rating += 0.1 return rating class SqliteConsoleLexer(Lexer): """ Lexer for example sessions using sqlite3. """ name = 'sqlite3con' aliases = ['sqlite3'] filenames = ['*.sqlite3-console'] mimetypes = ['text/x-sqlite3-console'] url = 'https://www.sqlite.org' version_added = '0.11' def get_tokens_unprocessed(self, data): sql = SqlLexer(**self.options) curcode = '' insertions = [] for match in line_re.finditer(data): line = match.group() prompt_match = sqlite_prompt_re.match(line) if prompt_match is not None: insertions.append((len(curcode), [(0, Generic.Prompt, line[:7])])) insertions.append((len(curcode), [(7, Whitespace, ' ')])) curcode += line[8:] else: if curcode: yield from do_insertions(insertions, sql.get_tokens_unprocessed(curcode)) curcode = '' insertions = [] if line.startswith('SQL error: '): yield (match.start(), Generic.Traceback, line) else: yield (match.start(), Generic.Output, line) if curcode: yield from do_insertions(insertions, sql.get_tokens_unprocessed(curcode)) class RqlLexer(RegexLexer): """ Lexer for Relation Query Language. """ name = 'RQL' url = 'http://www.logilab.org/project/rql' aliases = ['rql'] filenames = ['*.rql'] mimetypes = ['text/x-rql'] version_added = '2.0' flags = re.IGNORECASE tokens = { 'root': [ (r'\s+', Whitespace), (r'(DELETE|SET|INSERT|UNION|DISTINCT|WITH|WHERE|BEING|OR' r'|AND|NOT|GROUPBY|HAVING|ORDERBY|ASC|DESC|LIMIT|OFFSET' r'|TODAY|NOW|TRUE|FALSE|NULL|EXISTS)\b', Keyword), (r'[+*/<>=%-]', Operator), (r'(Any|is|instance_of|CWEType|CWRelation)\b', Name.Builtin), (r'[0-9]+', Number.Integer), (r'[A-Z_]\w*\??', Name), (r"'(''|[^'])*'", String.Single), (r'"(""|[^"])*"', String.Single), (r'[;:()\[\],.]', Punctuation) ], }