jgm / skylighting

A Haskell syntax highlighting library with tokenizers derived from KDE syntax highlighting descriptions
195 stars 63 forks source link

add SPARQL & Turtle highlighter #44

Open VladimirAlexiev opened 6 years ago

VladimirAlexiev commented 6 years ago

See https://www.w3.org/TR/sparql11-query/#sparqlGrammar

I'm thinking of using this as a model: https://github.com/jgm/skylighting/blob/master/skylighting-core/xml/sql.xml @jgm, could you give me some pointers? I have a lot of questions (eg what is #stay and #pop, what is the list of reusable itemDatas...)

Guess I should first read https://docs.kde.org/stable5/en/applications/katepart/highlight.html, then diff https://github.com/KDE/syntax-highlighting/blob/master/data/syntax/sql.xml to the above to see what changes skylighting has compared to KDE/syntax-highlighting?

My next question would be about Turtle: https://www.w3.org/TR/turtle/#h3_sec-grammar-grammar. It has very few keywords and relatively simple structure that consists mostly of terms.

Thanks for all your help!

VladimirAlexiev commented 6 years ago

Emacs does it with regexps:

(defconst sparql-keywords-re
  (regexp-opt
   '("ADD" "ALL" "AS" "ASC" "ASK"
     "BASE" "BIND" "BINDINGS" "BY"
     "CLEAR" "CONSTRUCT" "COPY" "CREATE"
     "DATA" "DEFAULT" "DELETE" "DESC" "DESCRIBE" "DISTINCT" "DROP"
     "EXISTS"
     "FILTER" "FROM"
     "GRAPH" "GROUP"
     "HAVING"
     "IN" "INSERT" "INTO"
     "LIMIT" "LOAD"
     "MINUS" "MOVE"
     "NAMED" "NOT"
     "OFFSET" "OPTIONAL" "ORDER"
     "PREFIX"
     "REDUCED"
     "SELECT" "SERVICE" "SILENT"
     "TO"
     "UNDEF" "UNION" "USING"
     "WHERE" "WITH")
   'symbols))

(defconst sparql-keywords
  `((,(rx "<" (* (not space)) ">")
     0 font-lock-constant-face t)
    (,(rx bol (*? (not (in "#"))) (group "\"" (* (not (in "\""))) "\""))
     1 font-lock-string-face)
    (,(rx bol (*? (not (in "#"))) (group "'" (* (not (in "'"))) "'"))
     1 font-lock-string-face)
    (,(rx (*? not-newline) (group "#" (* not-newline)))
     1 font-lock-comment-face)
    ,sparql-keywords-re
    (,(rx (any "?$") (+ word))
     0 font-lock-variable-name-face)))

Turtle (n3) is basically a subset of sparql:

(setq n3-highlights
  '(("\\(@prefix\\)\\>" 1 font-lock-keyword-face t)
    ("\\(a\\)\\>" 1 font-lock-keyword-face t)
    ("\\(\\S-*?:\\)" 1 font-lock-type-face t)
    (":\\(.+?\\)[ ;.]" 1 font-lock-constant-face t)
    ("\\(<.*?>\\)" 1 font-lock-function-name-face t)
    ("\\(\\\".*?\\\"\\)" 1 font-lock-string-face t)
    ; Bug: some trailing characters are highlighted; restricting comments regexp
    ; ("\\(#.*\\)" 1 font-lock-comment-face t)
    ("^\\s-*\\(#.*\\)" 1 font-lock-comment-face t)
    )
)
jgm commented 6 years ago

Yes, read the KDE docs. skylighting is nearly 100% compatible with existing KDE syntax definitions -- there are just occasionally small tweaks needed due to differences in the regex engine.

VladimirAlexiev commented 1 year ago

There is Turtle & SPARQL highlighting in highlight.js.

@jgm, what are the disadvantages of using highlight.js instead of skylighting ?

jgm commented 1 year ago

Skylighting will give you coloring support in many of the formats pandoc supports, and the coloring will be baked into the document (not requiring a javascript interpreter). But feel free to use highlighting.js if it meets your needs!