all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* List-of-lists data structure
@ 2014-04-12 13:46 Jacob Gerlach
  2014-04-12 20:07 ` Stefan Monnier
                   ` (2 more replies)
  0 siblings, 3 replies; 7+ messages in thread
From: Jacob Gerlach @ 2014-04-12 13:46 UTC (permalink / raw)
  To: help-gnu-emacs

I'm working on implementing fontification in a major mode for an in
house scripting language. The scripts configure any of about 50 different
apps, each of which has different keywords. This looks something like:

--------------------------------
processConfig = pFoo
{
  foo = 1
  bar = true
  baz = normal
}

processConfig = pBar
{
  apples  = foo
  pears   = bar
  oranges = false
}
---------------------------------

Only the names on the left of the equals are keywords, so foo and bar
should receive the appropriate font lock face in the pFoo block, but
not in the pBar block (where apples, pears, and oranges would be
font-locked).

I have managed to make fontification app specific using multiline font
lock matching and regexp's anchored to "processConfig = pFoo" (for
example).

In the interest of long term maintainability, I want to create a data
structure to store all of the app names and associated keywords, which
I then iterate through to generate the entries for the fontification
keyword list.

For one app, I can use:

(setq name-and-keyword-list
      '("pFoo" ("foo" "bar" "baz")))

This gives me the ability to do something like:

(while foo
  (add-to-list 'my-app-names (car foo))
  (add-to-list 'my-keywords (make-font-lock-cons (car foo) (cdr foo)))
  (setq foo (cdr foo)))

Where make-font-lock-cons is a hypothetical function that returns the
appropriate cons cell for my keyword list, which uses the app name
(the car) as the anchor, and uses regexp-opt to generate a regexp for
actually matching using the list of keywords (the cdr).

This works for my one entry list - at least, (car name-and-keyword-list)
and (cdr name-and-keyword-list) give me what I would expect.

However, if I try to expand the list:

(setq name-and-keyword-list
      '("pFoo" ("foo" "bar" "baz"))
      '("pBar" ("apples" "pears" "orange")))

Building the list gives:
Wrong type argument: symbolp, (quote ("pBar" ("apples" "pears"
"orange")))

Is this a simple syntax error, or am I approaching this data structure
in the wrong way?

Any other recommendations on my approach to this problem would be very
welcome.

Thanks,
Jake


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: List-of-lists data structure
  2014-04-12 13:46 List-of-lists data structure Jacob Gerlach
@ 2014-04-12 20:07 ` Stefan Monnier
  2014-04-13  8:13 ` Thien-Thi Nguyen
       [not found] ` <mailman.19545.1397376560.10748.help-gnu-emacs@gnu.org>
  2 siblings, 0 replies; 7+ messages in thread
From: Stefan Monnier @ 2014-04-12 20:07 UTC (permalink / raw)
  To: help-gnu-emacs

> Only the names on the left of the equals are keywords,

Any reason you don't just use something like

   (defvar foo-mode-font-lock-keywords
     '(("^[ \t]*\\(\\(?:\\sw\\|\\s_\\)+\\)[ \t]*=" (1 font-lock-keyword))))


-- Stefan




^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: List-of-lists data structure
       [not found] <mailman.19516.1397327481.10748.help-gnu-emacs@gnu.org>
@ 2014-04-13  7:49 ` Joost Kremers
  2014-04-13 18:44   ` Jacob Gerlach
  0 siblings, 1 reply; 7+ messages in thread
From: Joost Kremers @ 2014-04-13  7:49 UTC (permalink / raw)
  To: help-gnu-emacs

Jacob Gerlach wrote:
> However, if I try to expand the list:
>
> (setq name-and-keyword-list
>       '("pFoo" ("foo" "bar" "baz"))
>       '("pBar" ("apples" "pears" "orange")))
>
> Building the list gives:
> Wrong type argument: symbolp, (quote ("pBar" ("apples" "pears"
> "orange")))

The error message (which IMHO is a bit cryptic), says that it is
expecting a symbol but getting a list. The point is, your setq is wrong.
The arguments of setq need to be pairs of [symbol value]. You specify a
symbol and then two lists. What you (apparently) want is a list of
lists:

(setq name-and-keyword-list
      '(("pFoo" ("foo" "bar" "baz"))
        ("pBar" ("apples" "pears" "orange"))))

However, I was wondering the same thing as Stefan: why not use the
=-sign to anchor your font-lock entry? Seems much easier.

-- 
Joost Kremers                                   joostkremers@fastmail.fm
Selbst in die Unterwelt dringt durch Spalten Licht
EN:SiS(9)


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: List-of-lists data structure
  2014-04-12 13:46 List-of-lists data structure Jacob Gerlach
  2014-04-12 20:07 ` Stefan Monnier
@ 2014-04-13  8:13 ` Thien-Thi Nguyen
       [not found] ` <mailman.19545.1397376560.10748.help-gnu-emacs@gnu.org>
  2 siblings, 0 replies; 7+ messages in thread
From: Thien-Thi Nguyen @ 2014-04-13  8:13 UTC (permalink / raw)
  To: help-gnu-emacs

[-- Attachment #1: Type: text/plain, Size: 1456 bytes --]

() Jacob Gerlach <jacobgerlach@gmail.com>
() Sat, 12 Apr 2014 09:46:47 -0400

   (setq name-and-keyword-list
         '("pFoo" ("foo" "bar" "baz")))

Here, note that the car of ‘name-and-keyword-list’ is indeed "pFoo", a
single string, while the cdr is ‘(("foo" "bar" "baz"))’, a list of one
element, a sub-list of three string elements.  Is that what you want?

   (setq name-and-keyword-list
         '("pFoo" ("foo" "bar" "baz"))
         '("pBar" ("apples" "pears" "orange")))

   Is this a simple syntax error, or am I approaching this data
   structure in the wrong way?

Yes.  :-D

In the ‘setq’ docstring (via ‘C-h f setq RET’), the prototype is:

 (setq [SYM VAL]...)

which on first blush seems to imply that multiple VAL are possible, due
to the ellipses at the end of the form.  But that's not right!  You have
to distribute the ellipses properly, kind of like (A+D)C => AC+DC.  The
upshot is that each SYM has its own VAL.  That's the syntax error part.

The approaching the data structure in the wrong way part is a matter of
pov.  When you teach someone to dance, maybe left is right but right is
wrong (and maybe not!).  I think Perlis would appreciate your focus.

-- 
Thien-Thi Nguyen
   GPG key: 4C807502
   (if you're human and you know it)
      read my lisp: (responsep (questions 'technical)
                               (not (via 'mailing-list)))
                     => nil

[-- Attachment #2: Type: application/pgp-signature, Size: 197 bytes --]

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: List-of-lists data structure
  2014-04-13  7:49 ` Joost Kremers
@ 2014-04-13 18:44   ` Jacob Gerlach
  2014-04-13 18:59     ` Joost Kremers
  0 siblings, 1 reply; 7+ messages in thread
From: Jacob Gerlach @ 2014-04-13 18:44 UTC (permalink / raw)
  To: help-gnu-emacs

> However, I was wondering the same thing as Stefan: why not use the
> 
> =-sign to anchor your font-lock entry? Seems much easier.

Perhaps I'm misunderstanding, but if I tried to use apples in the pFoo configuration block, I'd like it not to be font-locked since it's only a valid keyword for pBar. If I anchored to the '=', wouldn't all keywords be fontified in all blocks?


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: List-of-lists data structure
       [not found] ` <mailman.19545.1397376560.10748.help-gnu-emacs@gnu.org>
@ 2014-04-13 18:47   ` Jacob Gerlach
  0 siblings, 0 replies; 7+ messages in thread
From: Jacob Gerlach @ 2014-04-13 18:47 UTC (permalink / raw)
  To: help-gnu-emacs

> The approaching the data structure in the wrong way part is a matter of
> 
> pov.  When you teach someone to dance, maybe left is right but right is
> 
> wrong (and maybe not!).  I think Perlis would appreciate your focus.

I think I could have made the structure simpler without the nested list - I bought myself into an extra (car ...) when using the data. But it was a good lesson for a novice.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: List-of-lists data structure
  2014-04-13 18:44   ` Jacob Gerlach
@ 2014-04-13 18:59     ` Joost Kremers
  0 siblings, 0 replies; 7+ messages in thread
From: Joost Kremers @ 2014-04-13 18:59 UTC (permalink / raw)
  To: help-gnu-emacs

Jacob Gerlach wrote:
>> However, I was wondering the same thing as Stefan: why not use the
>> 
>> =-sign to anchor your font-lock entry? Seems much easier.
>
> Perhaps I'm misunderstanding, but if I tried to use apples in the pFoo
> configuration block, I'd like it not to be font-locked since it's only
> a valid keyword for pBar. If I anchored to the '=', wouldn't all
> keywords be fontified in all blocks?

You mentioned in your original post that the names on the left of the
equal signs are keywords. The font lock entry that Stefan provided would
highlight everything before the equal sign and nothing after it.

Of course, if that's too simplistic for what you're trying to do, then
feel free to ignore the suggestion. :-)


-- 
Joost Kremers                                   joostkremers@fastmail.fm
Selbst in die Unterwelt dringt durch Spalten Licht
EN:SiS(9)


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2014-04-13 18:59 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-04-12 13:46 List-of-lists data structure Jacob Gerlach
2014-04-12 20:07 ` Stefan Monnier
2014-04-13  8:13 ` Thien-Thi Nguyen
     [not found] ` <mailman.19545.1397376560.10748.help-gnu-emacs@gnu.org>
2014-04-13 18:47   ` Jacob Gerlach
     [not found] <mailman.19516.1397327481.10748.help-gnu-emacs@gnu.org>
2014-04-13  7:49 ` Joost Kremers
2014-04-13 18:44   ` Jacob Gerlach
2014-04-13 18:59     ` Joost Kremers

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.