Yocto Project Documentation
 help / color / mirror / Atom feed
From: Trevor Woerner <twoerner@gmail.com>
To: docs@lists.yoctoproject.org
Subject: [PATCH 3/7] sphinx: add a lexer for kernel metadata SCC files
Date: Wed, 25 Nov 2026 10:45:31 -0500	[thread overview]
Message-ID: <20261125154535.3486104-4-twoerner@gmail.com> (raw)
In-Reply-To: <20261125154535.3486104-1-twoerner@gmail.com>

The kernel manual quotes SCC description files throughout, and none of them
can say what they are: Pygments has no lexer for the language and is
unlikely to want one, so they render as plain text.

SCC is bash. spp rewrites a few line-oriented directives, then scc
sources the result with one shell function declared per statement and
PATH unset. Description files use that freedom - conditionals on KARCH
are common in real metadata - so extend the bash lexer and mark the
vocabulary, read from the functions the tooling declares rather than
from the manual's reference section, which omits several.

AI-Generated: codex/claude-opus 5.5 (xhigh)
Signed-off-by: Trevor Woerner <twoerner@gmail.com>
---
 documentation/conf.py             |   3 +-
 documentation/sphinx/scc-lexer.py | 116 ++++++++++++++++++++++++++++++
 2 files changed, 118 insertions(+), 1 deletion(-)
 create mode 100644 documentation/sphinx/scc-lexer.py

diff --git a/documentation/conf.py b/documentation/conf.py
index 95bb24002544..210b4ab60595 100644
--- a/documentation/conf.py
+++ b/documentation/conf.py
@@ -81,7 +81,8 @@ extensions = [
     'sphinxcontrib.rsvgconverter',
     'yocto-vars',
     'bitbake-lexer',
-    'bitbake-placeholder'
+    'bitbake-placeholder',
+    'scc-lexer'
 ]
 
 # current_version = "dev"
diff --git a/documentation/sphinx/scc-lexer.py b/documentation/sphinx/scc-lexer.py
new file mode 100644
index 000000000000..dd1166ca7014
--- /dev/null
+++ b/documentation/sphinx/scc-lexer.py
@@ -0,0 +1,116 @@
+"""Lexer for kernel metadata SCC description files.
+
+SCC describes a BSP or a kernel feature: which branch to build on, which
+patches to apply and which configuration fragments to queue. The tooling
+that reads it is ``yocto-kernel-tools``, and the shape of the language
+follows from how that tooling works rather than from how it looks. ``spp``
+rewrites a few line-oriented directives, and then ``scc`` *sources the
+result as a bash script*, having first defined one shell function per
+statement and unset ``PATH`` so nothing else can run.
+
+So an SCC file is not a dialect of bash but bash itself, under a
+preprocessor: the ``spp`` directives have their own, non-shell semantics -
+``#define`` is a comment as far as bash is concerned - while everything
+surviving that pass is ordinary shell with a fixed vocabulary of commands.
+Real metadata relies on it, with conditionals on ``KARCH`` in particular,
+and a lexer built from the manual's statement list rejects them. This one
+extends the bash lexer and marks the vocabulary, so the shell half is
+right by construction and only the command list has to be got right.
+
+That list comes from the implementation: the functions ``scc`` defines
+itself, the functions it sources from ``scc-cmds/*.cmd``, and the
+directives ``spp`` matches before any of that runs. It is wider than the
+"SCC Description File Reference" section of ``kernel-dev/advanced.rst``,
+which omits ``mark``, ``merge``, ``tag``, ``prefix``, and the flags.
+"""
+from pygments.lexer import bygroups, inherit, words
+from pygments.lexers.shell import BashLexer
+from pygments.token import Comment, Keyword, Name, Text, Whitespace
+
+__version__ = '2.0'
+
+# Statements, as the tooling defines them. ``spp`` handles include, patch
+# and kconf itself; the rest are shell functions declared by scc and by
+# scc-cmds/*.cmd before the file is sourced.
+_COMMANDS = (
+    'branch', 'define', 'git', 'include', 'kconf', 'mark', 'merge',
+    'patch', 'prefix', 'reloc_dir', 'set_kernel_version', 'tag',
+)
+
+# spp matches this before bash ever sees the file, so it is a directive
+# wearing a comment's clothes and needs its own rule.
+_DIRECTIVES = ('#define',)
+
+# Words that qualify a statement rather than starting one: the kconf
+# fragment classes kconf.cmd tests for, and the flags spp looks for on an
+# include line.
+_FLAGS = (
+    'as_module', 'hardware', 'non-hardware', 'nocfg', 'nopatch',
+    'optional', 'required',
+)
+
+# spp recognises these only immediately ahead of kconf, at the margin.
+_LEADERS = ('as_module', 'force')
+
+# A statement sits at the margin. Reaching it reliably needs the rule
+# below that stops a run of whitespace at the end of a line.
+_MARGIN = r'^[ \t]*'
+
+
+class SccLexer(BashLexer):
+    """Kernel metadata SCC description files."""
+
+    name = 'SCC'
+    aliases = ['scc']
+    filenames = ['*.scc']
+    mimetypes = []
+    url = 'https://docs.yoctoproject.org/kernel-dev/advanced.html'
+
+    tokens = {
+        'root': [
+            # Both spellings of define name a variable, so mark it.
+            (r'(' + _MARGIN + r')(' + '|'.join(_DIRECTIVES) +
+             r')([ \t]+)([A-Za-z_]\w*)',
+             bygroups(Whitespace, Comment.Preproc, Whitespace,
+                      Name.Variable)),
+            (r'(' + _MARGIN + r')(define)([ \t]+)([A-Za-z_]\w*)',
+             bygroups(Whitespace, Keyword, Whitespace, Name.Variable)),
+
+            # A statement only starts a line. Elsewhere the same word is
+            # an argument: "branch" is a command at the margin and a
+            # subcommand in "git branch pvr master".
+            (r'(' + _MARGIN + r')((?:' + '|'.join(_LEADERS) +
+             r')(?=[ \t]))?([ \t]*)('
+             + '|'.join(_COMMANDS) + r')(?=[ \t]|$)',
+             bygroups(Whitespace, Name.Builtin, Whitespace, Keyword)),
+
+            (words(_FLAGS, prefix=r'(?<=[ \t])', suffix=r'(?=[ \t]|$)'),
+             Name.Builtin),
+
+            # Keep whitespace runs from crossing a line ending, so the
+            # next statement is still at the margin when it is reached.
+            (r'[ \t]*\n', Whitespace),
+
+            # The bash lexer's own copy of this rule swallows the newline
+            # and the indent behind it, which would leave a statement
+            # inside a conditional no longer at the margin and so
+            # unmarked. Match the words alone and leave the rest.
+            (r'\b(if|fi|else|while|in|do|done|for|then|return|function|'
+             r'case|select|break|continue|until|esac|elif)\b', Keyword),
+
+            # Cosmetic: patch filenames conventionally lead with a
+            # sequence number, and bash would colour that as a number.
+            (r'\d+(?=[\w./+-])', Text),
+
+            inherit,
+        ],
+    }
+
+
+def setup(app):
+    app.add_lexer('scc', SccLexer)
+    return {
+        'version': __version__,
+        'parallel_read_safe': True,
+        'parallel_write_safe': True,
+    }
-- 
2.50.0.173.g8b6f19ccfc3a


  parent reply	other threads:[~2026-11-25 15:45 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-25 15:45 [PATCH 0/7] sphinx: carry lexers for the languages the manuals show Trevor Woerner
2026-11-25 15:45 ` [PATCH 1/7] sphinx: carry a BitBake lexer as a local extension Trevor Woerner
2026-11-25 15:45 ` [PATCH 2/7] sphinx: add a lexer for BitBake with placeholders Trevor Woerner
2026-11-25 15:45 ` Trevor Woerner [this message]
2026-11-25 15:45 ` [PATCH 4/7] sphinx: add a lexer for wic kickstart files Trevor Woerner
2026-11-25 15:45 ` [PATCH 5/7] sphinx: add a lexer for pydevshell sessions Trevor Woerner
2026-11-25 15:45 ` [PATCH 6/7] sphinx: read a bare "#" in a console block as output Trevor Woerner
2026-11-25 15:45 ` [PATCH 7/7] sphinx: mark placeholders in console and plain-text examples Trevor Woerner
2026-11-26 16:33 ` [docs] [PATCH 0/7] sphinx: carry lexers for the languages the manuals show Antonin Godard
2026-11-26 22:26   ` Trevor Woerner

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261125154535.3486104-4-twoerner@gmail.com \
    --to=twoerner@gmail.com \
    --cc=docs@lists.yoctoproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox