Permalink
Browse files

JSON Lexer.

  • Loading branch information...
1 parent bf5b459 commit de8737c3e40787a261fe08d395b5208c21685bd5 @ethercrow ethercrow committed Sep 4, 2012
Showing with 90 additions and 3 deletions.
  1. +1 −0 yi/src/library/Yi/Config/Default.hs
  2. +76 −0 yi/src/library/Yi/Lexer/JSON.x
  3. +12 −3 yi/src/library/Yi/Modes.hs
  4. +1 −0 yi/yi.cabal
@@ -168,6 +168,7 @@ defaultConfig =
AnyMode pythonMode,
AnyMode rubyMode,
AnyMode javaMode,
+ AnyMode jsonMode,
AnyMode ireaderMode,
AnyMode svnCommitMode,
AnyMode gitCommitMode,
@@ -0,0 +1,76 @@
+-- -*- haskell -*-
+--
+-- Lexical syntax for JSON
+--
+
+{
+{-# OPTIONS -w #-}
+module Yi.Lexer.JSON (initState, alexScanToken, Token(..)) where
+import Yi.Lexer.Alex
+import Yi.Style
+}
+
+$whitechar = [\ \t\n\r\f\v]
+$special = [\(\)\,\;\[\]\`\{\}\:]
+
+$ascdigit = 0-9
+$unidigit = [] -- TODO
+$digit = [$ascdigit $unidigit]
+
+$large = [A-Z \xc0-\xd6 \xd8-\xde]
+$small = [a-z \xdf-\xf6 \xf8-\xff \_]
+$alpha = [$small $large]
+
+$ascsymbol = [\!\#\$\%\&\*\+\.\/\<\=\>\?\@\\\^\|\-\~]
+$unisymbol = [] -- TODO
+$symbol = [$ascsymbol $unisymbol] # [$special \_]
+
+$graphic = [$small $large $symbol $digit $special \"\']
+
+$nonzerodigit = 1-9
+$octit = 0-7
+$hexit = [0-9 A-F a-f]
+$idchar = [$alpha $digit]
+$symchar = [$symbol]
+
+@digits = $nonzerodigit $digit*
+@octits = "0" $octit
+@hexits = "0x" $hexit
+
+@integer = @digits | @octits | @hexits
+@longinteger = @integer
+@number = @integer | @longinteger
+
+$cntrl = [$large \@\[\\\]\^\_]
+@ascii = \^ $cntrl | NUL | SOH | STX | ETX | EOT | ENQ | ACK
+ | BEL | BS | HT | LF | VT | FF | CR | SO | SI | DLE
+ | DC1 | DC2 | DC3 | DC4 | NAK | SYN | ETB | CAN | EM
+ | SUB | ESC | FS | GS | RS | US | SP | DEL
+
+$charesc = [abfnrtv\\\"\'\&]
+@escape = \\ ($charesc | @ascii | @number)
+@string = $graphic # [\"\\] | " " | @escape
+
+json :-
+<0> {
+ $white+ { c defaultStyle }
+ null { c keywordStyle }
+ \" @string* \" { c stringStyle }
+ @number
+ | @number \. @number? { c numberStyle }
+ . { c defaultStyle }
+}
+
+{
+
+type HlState = Int
+type Token = StyleName
+
+stateToInit :: HlState -> Int
+stateToInit x = 0
+
+initState :: HlState
+initState = 0
+
+#include "common.hsinc"
+}
View
@@ -2,7 +2,7 @@
module Yi.Modes (TokenBasedMode, fundamentalMode,
cMode, objectiveCMode, cppMode, cabalMode,
srmcMode, ocamlMode, ottMode, gnuMakeMode,
- perlMode, pythonMode, javaMode, anyExtension,
+ perlMode, pythonMode, javaMode, jsonMode, anyExtension,
extensionOrContentsMatch, linearSyntaxMode,
svnCommitMode, hookModes, applyModeHooks,
lookupMode, whitespaceMode, removeAnnots,
@@ -35,6 +35,7 @@ import qualified Yi.Lexer.Ott as Ott
import qualified Yi.Lexer.Perl as Perl
import qualified Yi.Lexer.Python as Python
import qualified Yi.Lexer.Java as Java
+import qualified Yi.Lexer.JSON as JSON
import qualified Yi.Lexer.Ruby as Ruby
import qualified Yi.Lexer.Srmc as Srmc
import qualified Yi.Lexer.SVNCommit as SVNCommit
@@ -47,8 +48,10 @@ type TokenBasedMode tok = Mode (Tree (Tok tok))
type StyleBasedMode = TokenBasedMode StyleName
fundamentalMode :: Mode syntax
-svnCommitMode, cMode, objectiveCMode, cppMode, cabalMode, srmcMode, ottMode, gnuMakeMode, perlMode, pythonMode, javaMode, rubyMode :: StyleBasedMode
-ocamlMode :: TokenBasedMode (OCaml.Token)
+svnCommitMode, cMode, objectiveCMode, cppMode, cabalMode,
+ srmcMode, ottMode, gnuMakeMode, perlMode, pythonMode,
+ javaMode, jsonMode, rubyMode :: StyleBasedMode
+ocamlMode :: TokenBasedMode OCaml.Token
fundamentalMode = emptyMode
{
@@ -162,6 +165,12 @@ javaMode = (linearSyntaxMode Java.initState Java.alexScanToken id)
modeApplies = anyExtension ["java"]
}
+jsonMode = (linearSyntaxMode JSON.initState JSON.alexScanToken id)
+ {
+ modeName = "json",
+ modeApplies = anyExtension ["json"]
+ }
+
isMakefile :: FilePath -> String -> Bool
isMakefile path _contents = matches $ takeFileName path
where matches "Makefile" = True
View
@@ -141,6 +141,7 @@ library
Yi.Lexer.Haskell
Yi.Lexer.JavaScript
Yi.Lexer.Java
+ Yi.Lexer.JSON
Yi.Lexer.Latex
Yi.Lexer.LiterateHaskell
Yi.Lexer.GitCommit

0 comments on commit de8737c

Please sign in to comment.