Saturday, February 9, 2013

SinTax Overdue

Syntax highlighting in Vim sucks.

I know. I haven't done as much of it as you have, but I've heard your cries from my cubicle, your desperate pleas for compassion whispered over and again at your desk to an uncaring Vim, your brave face masking knots of terror while chatting at the water cooler, and your wails of despair in the toilet stall when you thought everyone else had gone. It's been hard watching you go through this and for long now I've wished there was something I could do to ease your burden, to still your trembling hand, wipe the tears from your keyboard and tell you that "It's going to be okay." Of course, I couldn't do that. Nobody could. I'd be lying and you know it. My words would come across as naive, empty platitudes at best or, at worst, taken for cruel taunts to mock your effort, halt your progress and rob you of your due standing among the Nerds of Vim.

That was how we all lived. That is... before THEY came!



SinTax is a Vim plugin that provides a DSL for crafting syntax highlighting files.

It takes this:


" Vim syntax plugin for filetype name.
" Maintainer: Barry Arthur <barry.arthur@gmail.com>
" Israel Chauca F. <israelchauca@gmail.com>
" Version: 0.1
" Description: Long description.
" Last Change: 2013-02-01
" License: Vim License (see :help license)
" Location: syntax/vrs.vim
" Website: https://github.com/Raimondi/vrs

name    vrs
case    ignore
spell   default

keyword vrsTodo    .Todo : TODO FIXME XXX

partial token \S\+\s\+

partial separator
    \%(
      \\}          # an_escaped_\}
    \|             # or
      [^}]         # anything_but_a_}
    \)

match vrsNameErr   .Error      contained : ^\%{token}

match vrsName      .Identifier contained : ^\w\+\s\+

match vrsFlavorErr .Error      contained
  \%(
    ^\%{token}     # if_the_line_starts_with_a_token
  \)\@<=           #   before
  \%{token}        # a_token

match vrsFlavor    .Type       contained
  \%(
    ^\%{token}     # if_the_line_starts_with_a_token
  \)\@<=           #   before
  \%{vrsName}      # a_vrsName

match vrsCompItem  .Normal     contained
    \w\+           # a_word
  \|               # or
    \d\+           # a_number
  \|               # or
    ,\@<=          # a_separator_if_proceeded_by_a_comma
    \%{separator}\+

" match a composition atom: \%{pattern-name,count,separator}
match vrsCompose   .PreProc    contained contains=vrsCompItem
  \\%{             # a_literal_\{
    \S\+           # a_word_(pattern-name)
    ,              # a_comma
    \d\+           # a_number_(count)
    ,              # a_comma
    \%{separator}* # an_optional_separator
  }                # and_a_literal_}

match vrsRegExp    .String     contains=vrsCompose contained
  \%(
    ^\%{token,2}   # if_the_line_starts_with_two_tokens
  \)\@<=           #   before
  .*               # anything

match vrsCommand              contains=vrsName,vrsFlavor,vrsNameErr,vrsFlavorErr,vrsRegExp,vrsComment
  ^\%{token,2}\S.* # a_line_with_three_'tokens'_minimum

match vrsContinued .String     contains=vrsComment
  ^\s\+\S.*        # a_line_with_leading_whitespace_and_a_'token'

match vrsComment   .Comment    containedin=ALL contains=vrsTodo
  \%(              # as_long_as
    \%(
      \\ \\        #   (ignoring_any_number_of_double_backslashes_(\\))
    \)*
    \\             #   a_backslash
  \)\@<!           # doesn't_precede
  #.*$             # a_literal_#_followed_by_anything

match vrsError     .Error
  ^                # any_line_starting_with
  [^a-zA-Z0-9_#\ ] # anything_that's_not_alphanumeric,_underscore,_hash_or_a_space
  .*               # and_anything_afterwards



And generates this:



" Vim syntax plugin for filetype name.
" Maintainer: Barry Arthur <barry.arthur@gmail.com>
" Israel Chauca F. <israelchauca@gmail.com>
" Version: 0.1
" Description: Long description.
" Last Change: 2013-02-01
" License: Vim License (see :help license)
" Location: syntax/vrs.vim
" Website: https://github.com/Raimondi/vrs

" Quit when a (custom) syntax file was already loaded
if exists("b:current_syntax")
  finish
endif

" Allow use of line continuation.
let s:save_cpo = &cpo
set cpo&vim

syntax case ignore
syntax spell default
syntax keyword vrsTodo  TODO FIXME XXX 
syntax match vrsNameErr /^\S\+\s\+/ contained 
syntax match vrsName /^\w\+\s\+/ contained 
syntax match vrsFlavorErr /\%(^\S\+\s\+\)\@<=\S\+\s\+/ contained
syntax match vrsFlavor /\%(^\S\+\s\+\)\@<=^\w\+\s\+/ contained
" match a composition atom: \%{pattern-name,count,separator}
syntax match vrsCompItem /\w\+\|\d\+\|,\@<=\%(\\}\|[^}]\)\+/ contained
syntax match vrsCompose /\\%{\S\+,\d\+,\%(\\}\|[^}]\)*}/ contained contains=vrsCompItem
syntax match vrsRegExp /\%(^\S\+\s\+\S\+\s\+\)\@<=.*/ contains=vrsCompose contained
syntax match vrsCommand /^\S\+\s\+\S\+\s\+\S.*/ contains=vrsName,vrsFlavor,vrsNameErr,vrsFlavorErr,vrsRegExp,vrsComment
syntax match vrsContinued /^\s\+\S.*/ contains=vrsComment
syntax match vrsComment /\%(\%(\\\\\)*\\\)\@<!#.*$/ containedin=ALL contains=vrsTodo
syntax match vrsError /^[^a-zA-Z0-9_#\ ].*/ 
syntax match vrsError /foo/ 

hi def link vrsTodo Todo
hi def link vrsNameErr Error
hi def link vrsName Identifier
hi def link vrsFlavorErr Error
hi def link vrsFlavor Type
hi def link vrsCompItem Normal
hi def link vrsCompose PreProc
hi def link vrsRegExp String
hi def link vrsContinued String
hi def link vrsComment Comment
hi def link vrsError Error
hi def link vrsError Error

let b:current_syntax = "vrs"

let &cpo = s:save_cpo
unlet s:save_cpo

" vim: set sw=2 sts=2 et fdm=marker:



Now I feel more equipped to take your hand and say, "there, there."

Depends On: Raimondi's VimRegStyle
Coming Soon! SinTax is still in development but is ready for user testing. Feedback welcome.

No comments:

Post a Comment