Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions gf_fmt.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"review/",
"app/",
"web-search/",
"highlight/",
"tests/"
],
"exclude": [
Expand Down
5 changes: 5 additions & 0 deletions gfproject.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@
"agent", "code"
],
"mode": "r7rs"
},
"highlight": {
"root": "highlight/",
"depends-on": [],
"mode": "r7rs"
}
},
"tools": {
Expand Down
197 changes: 197 additions & 0 deletions highlight/liii/highlight/code-block.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
;;
;; Copyright (C) 2026 The Goldfish Scheme Authors
;;
;; Licensed under the Apache License, Version 2.0 (the "License");
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;;
;; http://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
;; WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
;; License for the specific language governing permissions and limitations
;; under the License.
;;

;; Markdown 围栏代码块的纯解析与语言解析工具,以及 IncludeRules 的子定义缓存。
;; 这些函数不触及高亮状态机与上下文栈,从 (liii highlight highlighter) 拆出,
;; 使主模块控制在 gfcheck 单文件行数上限内。

(define-library (liii highlight code-block)
(import (scheme base)
(scheme char)
(liii base)
(liii string)
(liii hash-table)
(liii highlight definition)
) ;import
(export %extract-first-word %normalize-code-lang %resolve-sub-definition
%rest-all-spaces? %code-block-close-line? %code-block-open-info
%parse-code-block-open fenced-code-frame?
) ;export
(begin

(define *embedded-def-cache* (make-hash-table))

(define (%extract-first-word str)
(let ((len (string-length str)))
(let loop
((i 0))
(cond ((= i len) "")
((char-whitespace? (string-ref str i)) (loop (+ i 1)))
(else
(let wloop
((j i))
(if (or (= j len) (char-whitespace? (string-ref str j)))
(substring str i j)
(wloop (+ j 1))
) ;if
) ;let
) ;else
) ;cond
) ;let
) ;let
) ;define

(define (%normalize-code-lang raw-lang)
(let* ((trimmed (string-trim-both raw-lang)) (lower (string-downcase trimmed)))
(cond ((or (string=? lower "scheme") (string=? lower "scm") (string=? lower "ss"))
"scheme"
) ;
((or (string=? lower "c") (string=? lower "h")) "c")
((or (string=? lower "cpp")
(string=? lower "c++")
(string=? lower "cxx")
(string=? lower "cc")
(string=? lower "hpp")
(string=? lower "hxx")
(string=? lower "hh")
) ;or
"cpp"
) ;
((string=? lower "json") "json")
((string=? lower "lua") "lua")
((or (string=? lower "markdown") (string=? lower "md")) "markdown")
((or (string=? lower "javascript")
(string=? lower "js")
(string=? lower "mjs")
(string=? lower "cjs")
(string=? lower "node")
) ;or
"javascript"
) ;
((or (string=? lower "bash")
(string=? lower "sh")
(string=? lower "shell")
(string=? lower "zsh")
) ;or
"bash"
) ;
((string-null? lower) #f)
(else lower)
) ;cond
) ;let*
) ;define

(define (%resolve-sub-definition lang-name)
(if (or (not (string? lang-name)) (string-null? lang-name))
#f
(let ((norm (%normalize-code-lang lang-name)))
(if (not norm)
#f
(if (hash-table-contains? *embedded-def-cache* norm)
(hash-table-ref *embedded-def-cache* norm)
(guard (err (else (hash-table-set! *embedded-def-cache* norm #f) #f))
(let ((d (load-syntax-definition norm)))
(hash-table-set! *embedded-def-cache* norm d)
d
) ;let
) ;guard
) ;if
) ;if
) ;let
) ;if
) ;define

(define (%rest-all-spaces? text k len)
(let loop
((i k))
(cond ((= i len) #t)
((let ((c (string-ref text i)))
(or (char=? c #\space) (char=? c #\tab))
) ;let
(loop (+ i 1))
) ;
(else #f)
) ;cond
) ;let
) ;define

(define (%code-block-close-line? text fence-char fence-len)
(let ((len (string-length text)))
(let loop
((i 0) (spaces 0))
(cond
((and (< i len) (char=? (string-ref text i) #\space) (< spaces 3))
(loop (+ i 1) (+ spaces 1))
) ;
((>= i len) #f)
((char=? (string-ref text i) fence-char)
(let count-loop
((j i) (cnt 0))
(if (and (< j len) (char=? (string-ref text j) fence-char))
(count-loop (+ j 1) (+ cnt 1))
(and (>= cnt fence-len) (%rest-all-spaces? text j len))
) ;if
) ;let
) ;
(else #f)
) ;cond
) ;let
) ;let
) ;define

(define (%code-block-open-info text j len ch cnt)
(let ((info (substring text j len)))
(if (and (char=? ch #\`) (string-index info #\`))
#f
(list ch cnt (%extract-first-word info))
) ;if
) ;let
) ;define

(define (%parse-code-block-open text)
(let ((len (string-length text)))
(let loop
((i 0) (spaces 0))
(cond
((and (< i len) (char=? (string-ref text i) #\space) (< spaces 3))
(loop (+ i 1) (+ spaces 1))
) ;
((>= i len) #f)
(else
(let ((ch (string-ref text i)))
(if (or (char=? ch #\`) (char=? ch #\~))
(let count-loop
((j i) (cnt 0))
(if (and (< j len) (char=? (string-ref text j) ch))
(count-loop (+ j 1) (+ cnt 1))
(if (< cnt 3) #f (%code-block-open-info text j len ch cnt))
) ;if
) ;let
#f
) ;if
) ;let
) ;else
) ;cond
) ;let
) ;let
) ;define

(define (fenced-code-frame? frame)
(and (pair? frame) (eq? (car frame) 'fenced-code))
) ;define

) ;begin
) ;define-library
135 changes: 135 additions & 0 deletions highlight/liii/highlight/context-stack.scm
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
;;
;; Copyright (C) 2026 The Goldfish Scheme Authors
;;
;; Licensed under the Apache License, Version 2.0 (the "License");
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;;
;; http://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
;; WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
;; License for the specific language governing permissions and limitations
;; under the License.
;;

;; 高亮上下文栈切换与栈顶查询工具。
;; 这些函数只操作纯上下文名栈与 include 帧,不构造/读取高亮状态机记录,
;; 从 (liii highlight highlighter) 拆出,使主模块控制在行数上限内。

(define-library (liii highlight context-stack)
(import (scheme base) (liii base) (liii highlight definition))
(export apply-context-switch apply-context-switch-simple
apply-context-switch-host %include-frame? %top-context
) ;export
(begin

;; 执行上下文切换
;; ctx-target: '(stay) | '(push "Name") | '(pop N) | '(pop N "Target")
;; stack: 顶层可能是普通字符串上下文名,也可能是 include 帧
;; (include <sub-def> <inner-stack>)
(define (apply-context-switch stack target def)
(cond
((and (pair? stack) (pair? (car stack)) (eq? (caar stack) 'include))
;; 顶层在 include 帧内:对 inner-stack 应用切换
(let* ((frame (car stack))
(sub-def (cadr frame))
(inner (caddr frame))
(new-inner (apply-context-switch-inner inner target sub-def))
) ;
(if (null? new-inner)
;; inner 栈弹空:丢弃 include 帧,回到外层
(if (null? (cdr stack))
(list (definition-initial-context-name def))
(cdr stack)
) ;if
(cons (list 'include sub-def new-inner (cadddr frame) (car (cddddr frame)))
(cdr stack)
) ;cons
) ;if
) ;let*
) ;
(else (apply-context-switch-simple stack target def))
) ;cond
) ;define

;; 对普通(纯上下文名字符串)栈应用切换
(define (apply-context-switch-simple stack target def)
(case (car target)
((stay) stack)
((push) (cons (cadr target) stack))
((pop)
(let* ((count (cadr target))
(target-name (if (> (length target) 2) (caddr target) #f))
;; 弹出 count 个,但保证至少保留底层的初始上下文
(popped
(let loop
((s stack) (n count))
(if (or (<= n 0) (null? (cdr s))) s (loop (cdr s) (- n 1)))
) ;let
) ;popped
) ;
(if target-name (cons target-name popped) popped)
) ;let*
) ;
(else stack)
) ;case
) ;define

;; 对 include 帧内部的 inner-stack 应用切换(inner-stack 全是字符串)
(define (apply-context-switch-inner inner target sub-def)
(case (car target)
((stay) inner)
((push) (cons (cadr target) inner))
((pop)
(let* ((count (cadr target))
(target-name (if (> (length target) 2) (caddr target) #f))
(popped
(let loop
((s inner) (n count))
(if (or (<= n 0) (null? s)) s (loop (cdr s) (- n 1)))
) ;let
) ;popped
) ;
(if (and target-name (not (null? popped))) (cons target-name popped) popped)
) ;let*
) ;
(else inner)
) ;case
) ;define

;; 宿主规则命中时使用:宿主规则在 include 帧内命中意味着要离开子语言,
;; 因此始终丢弃 include 帧,对外层栈应用切换。
(define (apply-context-switch-host stack target def)
(let* ((frame? (and (pair? stack) (pair? (car stack)) (eq? (caar stack) 'include)))
(outer (if frame? (cdr stack) stack))
(new-outer (apply-context-switch-simple outer target def))
) ;
(if (null? new-outer) (list (definition-initial-context-name def)) new-outer)
) ;let*
) ;define

(define (%include-frame? x)
(and (pair? x) (eq? (car x) 'include))
) ;define

;; 取栈顶上下文与对应 def(处理 include 帧)
;; 返回 (values cur-ctx effective-def)
(define (%top-context stack def)
(let ((top (car stack)))
(if (%include-frame? top)
(let* ((sub-def (cadr top))
(inner (caddr top))
(inner-top (if (null? inner) #f (car inner)))
(inner-ctx (if inner-top (definition-find-context sub-def inner-top) #f))
) ;
(values inner-ctx sub-def)
) ;let*
(values (definition-find-context def top) def)
) ;if
) ;let
) ;define

) ;begin
) ;define-library
Loading
Loading