help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Using syntax tables to parse buffer content


From: Eric Abrahamsen
Subject: Using syntax tables to parse buffer content
Date: Tue, 18 May 2021 14:02:27 -0700
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

Hi!

I often find myself parsing buffer or file contents using regular
expressions, and would much rather be using lower-level character syntax
to do it, both for reasons of speed and correctness. I've been looking
into using syntax tables to assign certain classes to characters, and
using either basic stuff like `skip-syntax-forward', or maybe
`parse-partial-sexp', to pull substrings out of a buffer.

My main problem now is escaping: I don't know how to treat escaped
special characters as non-special. The simplest example is in vCard
parsing. A property line might look like this:

URL;TYPE=homepage:https\://mygreatpage.com/
   ^    ^        ^

I've indicated the significant characters above: they include semicolon,
colon, equals, and comma. The semicolon in the URL is escaped, and
shouldn't be treated specially. These characters don't seem to fit the
existing syntax classes, so I've considered defining my own categories
for them.

The manual mentions escape syntax characters (the "\" class), but
doesn't quite make it clear *what* it escapes: I'm guessing only
open/close parentheses, and string delimiters? Then there's character
quote (the "/" class), which says the following character will "lose its
normal syntactic meaning", but I can't get that to *do* anything.

For example, in a text-mode test buffer, I add the "/" syntax class to
?*, then put that character before a space character, thinking it might
negate the space's whitespace class. That doesn't happen, though, as
(skip-syntax-forward "^ ") still stops at the space.

What am I missing, and is this kind of custom escaping possible? I can
peek back at the previous character, but at that point it's not too
different from regexp parsing.

Thanks in advance!
Eric




reply via email to

[Prev in Thread] Current Thread [Next in Thread]