Previous Pages

Lexer Snippets

Style keywords

Style following occurrences of variable names declared with keyword "var".

local l = lexer
local token, style, color, word_match = l.token, l.style, l.color, l.word_match
local P, R, S = l.lpeg.P, l.lpeg.R, l.lpeg.S

local M = { _NAME = 'lexer_name' }

local ws = token('whitespace', l.space^1)
local variable_list = {}
local variable = token('variable', P('var') * P(function(patt, i)
   variable_list[patt:match('[%w_]+', i)] = true
   return i
 end) * l.word)
local potential_variable = P(function(patt, i)
   return variable_list[patt:match('[%w_]+', i)] and i or nil
 end) * token('variable', l.word)

-- identifiers
local identifier = token('identifier', l.word)

M._rules = {
  { 'whitespace', ws },
  { 'variable', variable },
  { 'potential_variable', potential_variable },
  { 'identifier', identifier },
  { 'any_char', l.any_char },
}

return M

Edited on Monday, March 12th 2012 at 5:49 PM.