~ubuntu-branches/ubuntu/lucid/highlighting-kate/lucid

« back to all changes in this revision

Viewing changes to Text/Highlighting/Kate/Syntax/Relaxngcompact.hs

  • Committer: Bazaar Package Importer
  • Author(s): Joachim Breitner
  • Date: 2009-07-29 02:56:50 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090729025650-wfbvr5xduppcpnfx
Tags: 0.2.5-2
Copy a rule from hlibrary.mk to cater for the non-standard documentation
package name

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
{- This module was generated from data in the Kate syntax highlighting file relaxngcompact.xml, version 0.2,
 
2
   by  Rintze Zelle -}
 
3
 
 
4
module Text.Highlighting.Kate.Syntax.Relaxngcompact ( highlight, parseExpression, syntaxName, syntaxExtensions ) where
 
5
import Text.Highlighting.Kate.Definitions
 
6
import Text.Highlighting.Kate.Common
 
7
import Text.ParserCombinators.Parsec
 
8
import Data.List (nub)
 
9
import Data.Map (fromList)
 
10
import Data.Maybe (fromMaybe)
 
11
 
 
12
-- | Full name of language.
 
13
syntaxName :: String
 
14
syntaxName = "RelaxNG-Compact"
 
15
 
 
16
-- | Filename extensions for this language.
 
17
syntaxExtensions :: String
 
18
syntaxExtensions = "*.rnc"
 
19
 
 
20
-- | Highlight source code using this syntax definition.
 
21
highlight :: String -> Either String [SourceLine]
 
22
highlight input =
 
23
  case runParser parseSource startingState "source" input of
 
24
    Left err     -> Left $ show err
 
25
    Right result -> Right result
 
26
 
 
27
-- | Parse an expression using appropriate local context.
 
28
parseExpression :: GenParser Char SyntaxState LabeledSource
 
29
parseExpression = do
 
30
  st <- getState
 
31
  let oldLang = synStLanguage st
 
32
  setState $ st { synStLanguage = "RelaxNG-Compact" }
 
33
  context <- currentContext <|> (pushContext "Normal Text" >> currentContext)
 
34
  result <- parseRules context
 
35
  updateState $ \st -> st { synStLanguage = oldLang }
 
36
  return result
 
37
 
 
38
parseSource = do 
 
39
  lineContents <- lookAhead wholeLine
 
40
  updateState $ \st -> st { synStCurrentLine = lineContents }
 
41
  result <- manyTill parseSourceLine eof
 
42
  return $ map normalizeHighlighting result
 
43
 
 
44
startingState = SyntaxState {synStContexts = fromList [("RelaxNG-Compact",["Normal Text"])], synStLanguage = "RelaxNG-Compact", synStCurrentLine = "", synStCharsParsedInLine = 0, synStCaseSensitive = True, synStKeywordCaseSensitive = True, synStCaptures = []}
 
45
 
 
46
parseSourceLine = manyTill parseExpressionInternal pEndLine
 
47
 
 
48
pEndLine = do
 
49
  newline <|> (eof >> return '\n')
 
50
  context <- currentContext
 
51
  case context of
 
52
    "Normal Text" -> return ()
 
53
    "Comments" -> (popContext >> return ())
 
54
    "String" -> return ()
 
55
    "Node Names" -> (popContext >> return ())
 
56
    "Definitions" -> (popContext >> return ())
 
57
    _ -> return ()
 
58
  lineContents <- lookAhead wholeLine
 
59
  updateState $ \st -> st { synStCurrentLine = lineContents, synStCharsParsedInLine = 0 }
 
60
 
 
61
withAttribute attr txt = do
 
62
  if null txt
 
63
     then fail "Parser matched no text"
 
64
     else return ()
 
65
  let style = fromMaybe "" $ lookup attr styles
 
66
  st <- getState
 
67
  let oldCharsParsed = synStCharsParsedInLine st
 
68
  updateState $ \st -> st { synStCharsParsedInLine = oldCharsParsed + length txt } 
 
69
  return (nub [style, attr], txt)
 
70
 
 
71
styles = [("Normal Text","Normal"),("Comments","Comment"),("String","String"),("Keywords","Keyword"),("Datatypes","DataType"),("Node Names","Others"),("Definitions","Function")]
 
72
 
 
73
parseExpressionInternal = do
 
74
  context <- currentContext
 
75
  parseRules context <|> (pDefault >>= withAttribute (fromMaybe "" $ lookup context defaultAttributes))
 
76
 
 
77
defaultAttributes = [("Normal Text","Normal Text"),("Comments","Comments"),("String","String"),("Node Names","Node Names"),("Definitions","Definitions")]
 
78
 
 
79
parseRules "Normal Text" = 
 
80
  do (attr, result) <- (((pFirstNonSpace >> pDetectChar False '#' >>= withAttribute "Comments") >>~ pushContext "Comments")
 
81
                        <|>
 
82
                        ((pDetectChar False '"' >>= withAttribute "String") >>~ pushContext "String")
 
83
                        <|>
 
84
                        ((pKeyword " \n\t.()!+,<=>%&*/;?[]^{|}~\\" ["default","datatypes","div","empty","external","grammar","include","inherit","list","mixed","namespace","notAllowed","parent","start","token"] >>= withAttribute "Keywords"))
 
85
                        <|>
 
86
                        ((pKeyword " \n\t.()!+,<=>%&*/;?[]^{|}~\\" ["attribute","element"] >>= withAttribute "Keywords") >>~ pushContext "Node Names")
 
87
                        <|>
 
88
                        ((pKeyword " \n\t.()!+,<=>%&*/;?[]^{|}~\\" ["string","text","xsd:anyURI","xsd:base64Binary","xsd:boolean","xsd:byte","xsd:date","xsd:dateTime","xsd:decimal","xsd:double","xsd:duration","xsd:ENTITIES","xsd:ENTITY","xsd:float","xsd:gDay","xsd:gMonth","xsd:gMonthDay","xsd:gYear","xsd:gYearMonth","xsd:hexBinary","xsd:ID","xsd:IDREF","xsd:IDREFS","xsd:int","xsd:integer","xsd:language","xsd:long","xsd:Name","xsd:NCName","xsd:negativeInteger","xsd:NMTOKEN","xsd:NMTOKENS","xsd:nonNegativeInteger","xsd:nonPositiveInteger","xsd:normalizedString","xsd:NOTATION","xsd:positiveInteger","xsd:QName","xsd:short","xsd:string","xsd:time","xsd:token","xsd:unsignedByte","xsd:unsignedInt","xsd:unsignedLong","xsd:unsignedShort"] >>= withAttribute "Datatypes"))
 
89
                        <|>
 
90
                        ((lookAhead (pRegExpr (compileRegex "[\\w\\.-]+[\\s]+=")) >> return ([],"") ) >>~ pushContext "Definitions"))
 
91
     return (attr, result)
 
92
 
 
93
parseRules "Comments" = 
 
94
  pzero
 
95
 
 
96
parseRules "String" = 
 
97
  do (attr, result) <- ((pDetectChar False '"' >>= withAttribute "String") >>~ (popContext >> return ()))
 
98
     return (attr, result)
 
99
 
 
100
parseRules "Node Names" = 
 
101
  do (attr, result) <- ((lookAhead (pDetectChar False '{') >> return ([],"") ) >>~ (popContext >> return ()))
 
102
     return (attr, result)
 
103
 
 
104
parseRules "Definitions" = 
 
105
  do (attr, result) <- ((lookAhead (pDetectChar False '=') >> return ([],"") ) >>~ (popContext >> popContext >> return ()))
 
106
     return (attr, result)
 
107
 
 
108
parseRules x = fail $ "Unknown context" ++ x