Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pygments syntax: improve cylc lexer #2274

Merged
Merged
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 59 additions & 37 deletions sphinx/ext/cylc_lang.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

from pygments.lexer import RegexLexer, bygroups, include
from pygments.token import (Name, Comment, Text, Operator, String,
Punctuation, Error, Keyword)
Punctuation, Error, Keyword, Other)


class CylcLexer(RegexLexer):
Expand All @@ -33,9 +33,18 @@ class CylcLexer(RegexLexer):
HEADING_TOKEN = Name.Tag
SETTING_TOKEN = Name.Variable
GRAPH_TASK_TOKEN = Keyword.Declaration
GRAPH_XTRIGGER_TOKEN = Keyword.Type
PARAMETERISED_TASK_TOKEN = Name.Builtin
EXTERNAL_SUITE_TOKEN = Name.Builtin.Pseudo
INTERCYCLE_OFFSET_TOKEN = Name.Builtin

EMPY_BLOCK_REGEX = r'@\{([^\{\}]+|\{[^\}]+\})+\}'
EMPY_BLOCK_REGEX = (
r'@\%(open)s(' # open empy block
r'[^\%(open)s\%(close)s]+|' # either not a close character
r'\%(open)s([^\%(close)s]+)?\%(close)s)+' # or permit 1 level nesting
r'\%(close)s') # close empy block

# Pygments values.
name = 'Cylc'
aliases = ['cylc', 'suiterc']
Expand All @@ -46,10 +55,7 @@ class CylcLexer(RegexLexer):
tokens = {
'root': [
# Jinja2 opening braces: {{ {% {#
include('jinja2-openers'),

# Jinja2 shebang: #!Jinja2
(r'#![Jj]inja2', Comment.Hashbang),
include('preproc'),

# Cylc comments: # ...
include('comment'),
Expand All @@ -75,25 +81,29 @@ class CylcLexer(RegexLexer):
Operator), 'inline-graph'),

# Multi-line settings: key = """ ...
(r'(.*)(\s+)?(=)([\s+])?(\"\"\")',
(r'([^=\n]+)(=)([\s+])?(\"\"\")',
bygroups(SETTING_TOKEN,
Text,
Operator,
Text,
String.Double), 'multiline-setting'),

# Inline settings: key = ...
(r'(.*)(\s+)?(=)',
(r'([^=\n]+)(=)',
bygroups(SETTING_TOKEN,
Text,
Operator), 'setting')
Operator), 'setting'),

# Include files
(r'(%include)( )(.*)', bygroups(Operator, Text, String)),

# Arbitrary whitespace
(r'\s', Text)
],

'heading': [
(r'[\]]+', HEADING_TOKEN, '#pop'),
include('jinja2-openers'),
include('preproc'),
include('parameterisation'),
(r'.', HEADING_TOKEN),
(r'(\\\n|.)', HEADING_TOKEN), # Allow line continuation chars.
],

# Cylc comments.
Expand All @@ -106,9 +116,8 @@ class CylcLexer(RegexLexer):

# The value in a key = value pair.
'setting': [

include('comment'),
include('jinja2-openers'),
include('preproc'),
(r'\\\n', String),
(r'.', String),

Expand All @@ -118,24 +127,37 @@ class CylcLexer(RegexLexer):
'multiline-setting': [
(r'\"\"\"', String.Double, '#pop'),
include('comment'),
include('jinja2-openers'),
include('preproc'),
(r'(\n|.)', String.Double)
],

# Graph strings: foo => bar & baz
'graph': [
include('jinja2-openers'),
include('preproc'),
include('comment'),
include('inter-suite-trigger'),
include('parameterisation'),
(r'@\w+', GRAPH_XTRIGGER_TOKEN),
(r'\w+', GRAPH_TASK_TOKEN),
(r'\!\w+', Other),
(r'\s', Text),
(r'=>', Operator),
(r'[\&\|\!]', Operator),
(r'[\&\|]', Operator),
(r'[\(\)]', Punctuation),
(r'\[', Text, 'intercycle-offset'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have noticed one omission. Dependencies on specific cycle points e.g. graph = "red[2019] => blue" are supported as valid specification within square brackets in a graph string, where only intercycle offsets appear to be recognised by the lexer as-is.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drat.

Turns out you can also do arithmetic (e.g. [^ + P1D]) which I didn't know. This is going to be a little trickier to support whilst maintaining error checking.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The good news is the lexer is now so water-tight it picked up on several missing markers in some of the Cylc documentation code excerpts. It's nearly there 👏

(r'.', Comment)
],

'inter-suite-trigger': [
(r'(\<)'
r'([^\>]+)' # foreign suite
r'(::)'
r'([^\>]+)' # foreign task
r'(\>)',
bygroups(Text, EXTERNAL_SUITE_TOKEN, Text,
PARAMETERISED_TASK_TOKEN, Text)),
],

# Parameterised syntax: <foo=1>
'parameterisation': [
(r'(\<)' # Opening greater-than bracket.
Expand Down Expand Up @@ -218,30 +240,30 @@ class CylcLexer(RegexLexer):
include('graph')
],

# Provides entry points for the other Jinja2 sections.
'jinja2-openers': [
(r'\{\{', Comment.Preproc, 'jinja2-inline'),
(r'\{\%', Comment.Preproc, 'jinja2-block'),
# Capture "{#" (jinja2) but not "${#" (bash).
(r'(?<!\$)\{#', Comment.Multi, 'jinja2-comment'),
],

# {# ... #}
'jinja2-comment': [
(r'#\}', Comment.Multi, '#pop'),
(r'(.|\n)', Comment.Multi)
'empy': [
(r'#![Ee]mpy', Comment.Hashbang), # #!empy
(r'@@', Text), # @@
# @[...]
(EMPY_BLOCK_REGEX % {'open': '(', 'close': ')'}, Comment.Preproc),
# @{...}
(EMPY_BLOCK_REGEX % {'open': '{', 'close': '}'}, Comment.Preproc),
# @(...)
(EMPY_BLOCK_REGEX % {'open': '[', 'close': ']'}, Comment.Preproc),
(r'@empy\.[\w]+[^\n]+', Comment.Preproc), # @empy...
(r'(\s+)?@#.*', Comment.Multi), # @# ...
(r'@[\w.]+', Comment.Preproc) # @...
],

# {% ... %}
'jinja2-block': [
(r'\%\}', Comment.Preproc, '#pop'),
(r'(.|\n)', Comment.Preproc)
'jinja2': [
(r'#![Jj]inja2', Comment.Hashbang), # #!jinja2
(r'\{\{((.|\n)+?)(?=\}\})\}\}', Comment.Preproc), # {{...}}
(r'\{\%((.|\n)+?)(?=\%\})\%\}', Comment.Preproc), # {%...%}
(r'\{\#((.|\n)+?)(?=\#\})\#\}', Comment.Multi), # {#...#}
],

# {{ ... }}
'jinja2-inline': [
(r'\}\}', Comment.Preproc, '#pop'),
(r'(.|\n)', Comment.Preproc)
'preproc': [
include('empy'),
include('jinja2')
]

}
Expand Down