parser

Colorer for Parser scripts

[October 06, 2003]

This class is designed for coloring Parser scripts.
It can be useful for those who make sites and forums dedicated to Parser.

How to use:

$pColor[^ipColorer::load[test.p]]
<pre>
^pColor.getString[]
</pre>

Class itself:

########################################################
##
## ipColorer
## version 1.0
##
## Copyright (c) 2003 by Oleg Volchkov
## e-mail: sumo@proc.ru
##
########################################################
@CLASS
ipColorer

@create[aStr]
## Class's constructor
## aStr          String with Parser code.
  $_str[^aStr.normalize[]]
  ^_makeLocal[]
  ^_tokenize[]

@load[aFileName]
## Class's constructor.
## loads file and processes received string.
## aFileName      name of file containing code in Parser.
  $_file[^file::load[text;$aFileName]]
  $_str[^taint[html][$_file.text]]
  ^_makeLocal[]
  ^_tokenize[]


@getString[]
## returns modified string.
  $result[$_str]


@getTable[]
## Returns modified as table (split into lines).
## It is useful if you want to provide line numbers.
  $result[^_str.split[^#0A][v]]


@_tokenize[][lNum;lComB;lComL]
## The method to color the code.
## Calls local methods for different types.

  $lUID[^math:uid64[]]
  
# Mark and "cut out" comments replacing them with unique identifier.
  $lComB[^_str.match[(\^^rem{ .*? })][gx]]
  ^if($lComB){
    $_str[^_str.match[\^^rem{ .*? }][gx]{/%b$lUID%/}]
  }

  $lComL[^_str.match[^^(\# .* )^$][gmx]]
  ^if($lComL){
    $_str[^_str.match[^^\# .* ^$][gmx]{/%l$lUID%/}]
  }

# HTML-tags
  $_str[^_str.match[(</? \w+\s? .*? /? >)][gx]{^_makeHTML[$match.1]}]

# Service constructions
  $_str[^_str.match[^^(@ (?:BASE|USE|CLASS) )^$][gmx]{^_makeService[$match.1]}]
# Methods definitions
  $_str[^_str.match[^^(@ [\w\-]+ \[ [\w^;\-]* \] (?:\[ [\w^;\-]* \])? ) (.*)^$][gmx]{^_makeMethodDefine[$match.1;$match.2]}]
# Method calls
  $_str[^_str.match[(\^^ [\w\-\.\:]+)][gx]{^_makeMethodCall[$match.1]}]

# Variables
  $_str[^_str.match[(\^$ \{? [\w\-\.\:]+ \}?)][gx]{^_makeVar[$match.1]}]

# Brackets
  $_str[^_str.match[([\[\]\{\}\(\)]+)][g]{^_makeBrackets[$match.1]}]

# Completing comments
  ^if($lComB){
     $_str[^_str.match[/%b$lUID%/][g]{^_makeComment[$lComB.1]^lComB.offset(1)}]
  }

  ^if($lComL){
     $_str[^_str.match[/%l$lUID%/][g]{^_makeComment[$lComL.1]^lComL.offset(1)}]
  }
  
##############################################################
# Processing language syntax...

@_makeComment[aStr]
  ^if(^aStr.left(2) eq "##"){
    $result[<font color="$_colors.inParser"><i>$aStr</i></font>]
  }{
     $result[<font color="$_colors.comment"><i>$aStr</i></font>]
   }

@_makeHTML[aStr]
  $result[<font color="$_colors.html">$aStr</font>]

@_makeService[aStr]
  $result[<font color="$_colors.service">$aStr</font>]

@_makeBrackets[aStr]
  $result[<font color="$_colors.brackets">$aStr</font>]

@_makeVar[aStr]
  ^if($aStr eq "^$result"){
    $result[<font color="$_colors.result">$aStr</font>]
  }{
    $result[<font color="$_colors.var">$aStr</font>]
   }

@_makeMethodDefine[aStr;aAdd]
  $result[<font color="$_colors.methodDefine"><b>$aStr</b></font>^_makeComment[$aAdd]]

@_makeMethodCall[aStr]
## Separating built-in methods calls from user methods calls
  ^if($_reservedWords.[^aStr.mid(1)] || ^aStr.left(6) eq "^^MAIN:" || ^aStr.left(6) eq "^^BASE:"){
    $result[<font color="$_colors.reservedWord">$aStr</font>]
  }{
     $result[<font color="$_colors.methodCall">$aStr</font>]
   }


@_makeLocal[]
## Defining service variables

# Colors
  $_colors[
     $.brackets[#0000AA]
     $.reservedWord[#0000AA]
     $.methodDefine[#990000]
     $.methodCall[#AA0000]
     $.html[#0077DD]
     $.service[#990000]
     $.var[#CC0000]
     $.result[#D27C00]
     $.comment[#888888]
     $.inParser[#555555]
  ]

# Reserved words
  $_reservedWords[
     $.if(1)
     $.switch(1)
     $.case(1)
     $.for(1)
     $.while(1)
     $.taint(1)
     $.untaint(1)
     $.try(1)
     $.throw(1)
     $.eval(1)
     $.process(1)
     $.cache(1)
     $.use(1)
     $.connect(1)
  ]
Download:

colorer.zip (07.10.2003 1.7KB)
Colorer for Parser scripts.