Commits

Jeffrey Arnold committed 0599ae0

cleaning up BugsLexer, JagsLexer

Comments (0)

Files changed (2)

pygments/lexers/math.py

             (r'#.*$', Comment.Single),
         ],
         'valid_name': [
-            (r'[a-zA-Z][0-9a-zA-Z\._]+', Text),
+            (r'[a-zA-Z][0-9a-zA-Z\._]*', Text),
             # can begin with ., but not if that is followed by a digit
-            (r'\.[a-zA-Z_][0-9a-zA-Z\._]+', Text),
+            (r'\.[a-zA-Z_][0-9a-zA-Z\._]*', Text),
         ],
         'punctuation': [
             (r'\[{1,2}|\]{1,2}|\(|\)|;|,', Punctuation),
             include('whitespace'),
             # Block start
             (r'(?s)(model)(\s|\n)+({)',
-             bygroups(Keyword.Namespace, Text, Punctuation), 'block')
-        ],
-        'block' : [
-            include('comments'),
-            include('whitespace'),
+             bygroups(Keyword.Namespace, Text, Punctuation)),
             # Reserved Words
-            (r'(for|in)\b', Keyword.Reserved),
+            (r'(for|in)(?![0-9a-zA-Z\._])', Keyword.Reserved),
             # Built-in Functions
             (r'(%s)(?=\s*\()'
              % r'|'.join(_FUNCTIONS + _DISTRIBUTIONS),
             # Number Literals
             (r'[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?', Number),
             # Punctuation
-            (r'(\[|\]|\(|\)|:|,)', Punctuation),
+            (r'\[|\]|\(|\)|:|,|;', Punctuation),
             # Assignment operators
             # SLexer makes these tokens Operators.
-            (r'(<-|~)', Operator),
+            (r'<-|~', Operator),
             # Infix and prefix operators
-            (r'(\+|-|\*|/)', Operator),
+            (r'\+|-|\*|/', Operator),
             # Block
-            (r'{', Punctuation, '#push'),
-            (r'}', Punctuation, '#pop'),
-            # Other
-            (r';', Punctuation),
+            (r'[{}]', Punctuation),
             ]
         }
 
     def analyse_text(text):
-        if re.match(r"^\s*model\s*{", text):
+        if re.search(r"^\s*model\s*{", text, re.M):
             return 0.7
         else:
-            return 0
+            return 0.0
 
 class JagsLexer(RegexLexer):
     """
             ],
         'names' : [
             # Regular variable names
-            (r'\b[A-Za-z][A-Za-z0-9_.]*\b', Name),
+            (r'[a-zA-Z][a-zA-Z0-9_.]*\b', Name),
             ],
         'comments' : [
             # do not use stateful comments
             include('whitespace'),
             # Block start
             (r'(?s)(model|data)(\s|\n)+({)',
-             bygroups(Keyword.Namespace, Text, Punctuation), 'block'),
-            # Variable declaration (TODO: improve)
-            (r'var\b', Keyword.Declaration, 'var')
-        ],
-        'statements': [
-            include('comments'),
-            include('whitespace'),
+             bygroups(Keyword.Namespace, Text, Punctuation)),
+            (r'var(?![0-9a-zA-Z\._])', Keyword.Declaration),
             # Reserved Words
-            (r'(for|in)\b', Keyword.Reserved),
+            (r'(for|in)(?![0-9a-zA-Z\._])', Keyword.Reserved),
             # Builtins
             # Need to use lookahead because . is a valid char
             (r'(%s)(?=\s*\()' % r'|'.join(_FUNCTIONS
             include('names'),
             # Number Literals
             (r'[-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?', Number),
-            (r'(\[|\]|\(|\)|:|,)', Punctuation),
+            (r'\[|\]|\(|\)|:|,|;', Punctuation),
             # Assignment operators
-            (r'(<-|~)', Operator),
+            (r'<-|~', Operator),
             # # JAGS includes many more than OpenBUGS
-            # |/|\|\||\&\&|>=?|<=?|[=!]?=|!|%.*?%|^)'
-            (r'(\+|-|\*|\/|\|\|[&]{2}|[<>=]=?|\^|%.*?%)', Operator),
-            ],
-        'block' : [
-            include('statements'),
-            (r';', Punctuation),
-            (r'{', Punctuation, '#push'),
-            (r'}', Punctuation, '#pop'),
-            ],
-        'var' : [
-            include('statements'),
-            (r';', Punctuation, '#pop'),
+            (r'\+|-|\*|\/|\|\|[&]{2}|[<>=]=?|\^|%.*?%', Operator),
+            (r'[{}]', Punctuation),
             ]
         }
 

tests/examplefiles/example.bug

 # Alligators: multinomial - logistic regression 
 #  http://www.openbugs.info/Examples/Aligators.html
 model {
-	   
-   # PRIORS	
+   # PRIORS    
    alpha[1] <- 0; # zero contrast for baseline food
    for (k in 2 : K) { 
-       alpha[k] ~ dnorm(0, 0.00001) # vague priors
+     alpha[k] ~ dnorm(0, 0.00001) # vague priors
    } 
    # Loop around lakes:
    for (k in 1 : K){ 
-        beta[1, k] <- 0 
+      beta[1, k] <- 0 
    } # corner-point contrast with first lake 
    for (i in 2 : I) { 
-	beta[i, 1] <- 0 ; # zero contrast for baseline food
-	for (k in 2 : K){ 
-	    beta[i, k] ~ dnorm(0, 0.00001) # vague priors
-	} 
+     beta[i, 1] <- 0 ; # zero contrast for baseline food
+     for (k in 2 : K){ 
+       beta[i, k] ~ dnorm(0, 0.00001) # vague priors
+     } 
    }
    # Loop around sizes:
    for (k in 1 : K){ 
-       gamma[1, k] <- 0 # corner-point contrast with first size 
+     gamma[1, k] <- 0 # corner-point contrast with first size 
    } 
    for (j in 2 : J) { 
-       gamma[j, 1] <- 0 ; # zero contrast for baseline food
-       for ( k in 2 : K){ 
-	    gamma[j, k] ~ dnorm(0, 0.00001) # vague priors
-       } 
+     gamma[j, 1] <- 0 ; # zero contrast for baseline food
+     for ( k in 2 : K){ 
+       gamma[j, k] ~ dnorm(0, 0.00001) # vague priors
+     } 
    }
 
    # LIKELIHOOD   
    for (i in 1 : I) { # loop around lakes
-       for (j in 1 : J) { # loop around sizes
+     for (j in 1 : J) { # loop around sizes
 
-           # Fit standard Poisson regressions relative to baseline
-       	   lambda[i, j] ~ dflat()   # vague priors 
-       	   for (k in 1 : K) { # loop around foods
-	       X[i, j, k] ~ dpois(mu[i, j, k])
-	       log(mu[i, j, k]) <- lambda[i, j] + alpha[k] + beta[i, k] + gamma[j, k]
-	       culmative.X[i, j, k] <- culmative(X[i, j, k], X[i, j, k])
-	   }
-       } 
+       # Fit standard Poisson regressions relative to baseline
+       lambda[i, j] ~ dflat()   # vague priors 
+       for (k in 1 : K) { # loop around foods
+           X[i, j, k] ~ dpois(mu[i, j, k])
+           log(mu[i, j, k]) <- lambda[i, j] + alpha[k] + beta[i, k] + gamma[j, k]
+           culmative.X[i, j, k] <- culmative(X[i, j, k], X[i, j, k])
+       }
+     }
    }
 
    # TRANSFORM OUTPUT TO ENABLE COMPARISON 
    # WITH AGRESTI'S RESULTS
    for (k in 1 : K) { # loop around foods
        for (i in 1 : I) { # loop around lakes
-	    b[i, k] <- beta[i, k] - mean(beta[, k]); # sum to zero constraint
+         b[i, k] <- beta[i, k] - mean(beta[, k]); # sum to zero constraint
        }
        for (j in 1 : J) { # loop around sizes
-	    g[j, k] <- gamma[j, k] - mean(gamma[, k]); # sum to zero constraint
+         g[j, k] <- gamma[j, k] - mean(gamma[, k]); # sum to zero constraint
        }
    }
 }