From 2b87516742cc90274f451ac05ea98b9b9b72d35d Mon Sep 17 00:00:00 2001 From: David Thompson Date: Tue, 19 Sep 2023 10:33:23 -0400 Subject: Add WAT lexer. --- Makefile.am | 1 + syntax-highlight/wat.scm | 60 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 syntax-highlight/wat.scm diff --git a/Makefile.am b/Makefile.am index e059e0f..9729dbc 100644 --- a/Makefile.am +++ b/Makefile.am @@ -47,6 +47,7 @@ SOURCES = \ syntax-highlight/javascript.scm \ syntax-highlight/lisp.scm \ syntax-highlight/scheme.scm \ + syntax-highlight/wat.scm \ syntax-highlight/xml.scm \ syntax-highlight.scm diff --git a/syntax-highlight/wat.scm b/syntax-highlight/wat.scm new file mode 100644 index 0000000..4966d05 --- /dev/null +++ b/syntax-highlight/wat.scm @@ -0,0 +1,60 @@ +;;; guile-syntax-highlight --- General-purpose syntax highlighter +;;; Copyright © 2023 David Thompson +;;; +;;; Guile-syntax-highlight is free software; you can redistribute it +;;; and/or modify it under the terms of the GNU Lesser General Public +;;; License as published by the Free Software Foundation; either +;;; version 3 of the License, or (at your option) any later version. +;;; +;;; Guile-syntax-highlight is distributed in the hope that it will be +;;; useful, but WITHOUT ANY WARRANTY; without even the implied +;;; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. +;;; See the GNU Lesser General Public License for more details. +;;; +;;; You should have received a copy of the GNU Lesser General Public +;;; License along with guile-syntax-highlight. If not, see +;;; . + +;;; Commentary: +;; +;; Syntax highlighting for WebAssembly text format. +;; +;;; Code: + +(define-module (syntax-highlight wat) + #:use-module (ice-9 match) + #:use-module (srfi srfi-1) + #:use-module (srfi srfi-11) + #:use-module (srfi srfi-26) + #:use-module (syntax-highlight lexers) + #:export (%default-special-symbols + %default-special-prefixes + make-wat-lexer + lex-wat)) + +(define char-set:wat-delimiters + (char-set-union char-set:whitespace + (char-set #\( #\)))) + +(define char-set:wat-symbol + (char-set-complement char-set:wat-delimiters)) + +(define %keywords + '("i31" "i32" "i64" "f32" "f64" "eq" "string" "module" "type" "func" + "array" "struct" "mut" "ref" "extern" "null" "field" "sub" "import" + "export" "param" "result" "block" "loop" "local" "rec")) + +;; TODO: WAT has some weird commenting syntax that this doesn't +;; support, among probably other missing things. +(define lex-wat + (lex-consume + (lex-any (lex-char-set char-set:whitespace) + (lex-tag 'open (lex-string "(")) + (lex-tag 'close (lex-string ")")) + (lex-tag 'comment (lex-delimited ";" #:until "\n")) + (lex-tag 'special + (lex-filter (lambda (str) + (any (cut string=? <> str) %keywords)) + (lex-char-set char-set:wat-symbol))) + (lex-tag 'string (lex-delimited "\"")) + (lex-tag 'symbol (lex-char-set char-set:wat-symbol))))) -- cgit v1.2.3