Changeset 1433 for trunk

Show
Ignore:
Timestamp:
02/04/12 13:46:19 (4 months ago)
Author:
stefan
Message:

added parsing of symbols

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/tools/src/jlatexeditor/tools/PackageParser.scala

    r1429 r1433  
    1313 */ 
    1414object PackageParser { 
    15   val DeclareOption = """.*\\DeclareOption\s*\{([^\}]+)}.*""".r 
    16   val DeclareOptionBeamer = """.*\\DeclareOptionBeamer\s*\{([^\}]+)}.*""".r 
    17   val RequirePackage = """.*\\RequirePackage\s*\{([^\}]+)}.*""".r 
    18   val Def = """.*\\(?:def|let)\s*\\(\w+)([#\[\]\d]*)\s*[\{\\].*""".r 
     15  val DeclareOption = """[^%]*\\DeclareOption\s*\{([^\}]+)}.*""".r 
     16  val DeclareOptionBeamer = """[^%]*\\DeclareOptionBeamer\s*\{([^\}]+)}.*""".r 
     17  val RequirePackage = """[^%]*\\RequirePackage\s*\{([^\}]+)}.*""".r 
     18  val Def = """[^%]*\\(?:def|let)\s*\\(\w+)([#\[\]\d]*)\s*[\{\\].*""".r 
    1919  val DefArgCount = """#+""".r 
    20   val NewCommand = """.*\\newcommand\{?\\(\w+)\}?(?:\[(\d+)\])?(?:\[([^\]]+)\])?.*""".r 
    21   val NewEnvironment = """.*\\newenvironment\{(\w+)\}(?:\[(\d+)\])?(?:\[([^\]]+)\])?.*""".r 
    22   val NewLength = """\\newlength\s*\{?\\(\w+)\}?.*""".r 
    23   val NewCounter = """\\newcounter\s*\{(\w+)\}.*""".r 
    24   val Input = """.*\\input\s*\{([^}]+)\}.*""".r 
     20  val NewCommand = """[^%]*\\newcommand\{?\\(\w+)\}?(?:\[(\d+)\])?(?:\[([^\]]+)\])?.*""".r 
     21  val DeclareSymbol = """[^%]*\\Declare(?:\w*)Symbol\s*\{?\\(\w+)\}?.*""".r 
     22  val NewEnvironment = """[^%]*\\newenvironment\{(\w+)\}(?:\[(\d+)\])?(?:\[([^\]]+)\])?.*""".r 
     23  val NewLength = """[^%]*\\newlength\s*\{?\\(\w+)\}?.*""".r 
     24  val NewCounter = """[^%]*\\newcounter\s*\{(\w+)\}.*""".r 
     25  val Input = """[^%]*\\input\s*\{([^}]+)\}.*""".r 
    2526  val DpkgResult = """([\w\.-]+): .*""".r 
    2627  val CtanPackSplit = """([^=]+)=(.*)""".r 
     
    142143  def parseFile (pack: Package, file: File) { 
    143144    for(line <- file.readLines) { 
    144       line match { 
    145         case DeclareOption(option) => 
    146           pack.options += option 
    147         case DeclareOptionBeamer(option) => 
    148           pack.options += option 
    149         case RequirePackage(packList) => 
    150           pack.requiresPackages ++= packList.split(",").toList.map(_.trim()).filterNot(_.contains("<")) 
    151         case Def(cmd, args) => 
    152           pack.commands += cmd -> new Command(pack, cmd, DefArgCount.findAllIn(args).size) 
    153           if (cmd.startsWith("end")) { 
    154             val envName = cmd.substring(3) 
    155             for (startCmd <- pack.commands.get(envName)) { 
    156               pack.environments += envName -> new Environment(pack, envName, startCmd.argCount, startCmd.optionalArgs) 
    157             } 
    158           } 
    159         case NewCommand(cmd, args, optArg) => 
    160           val argCount = if (args == null) 0 else args.toInt 
    161           pack.commands += cmd -> new Command(pack, cmd, argCount, if (optArg == null) List() else List(optArg)) 
    162         case NewEnvironment(cmd, args, optArg) => 
    163           val argCount = if (args == null) 0 else args.toInt 
    164           pack.environments += cmd -> new Environment(pack, cmd, argCount, if (optArg == null) List() else List(optArg)) 
    165         case NewLength(len) => 
    166           pack.lengths += len 
    167         case NewCounter(counter) => 
    168           pack.counters += counter 
    169         case Input(fileName) => 
    170           try { 
    171             if (!processedFiles.contains(fileName)) { 
    172               processedFiles += fileName 
    173               //println("  input ." + fileName + ".") 
    174               parseFile(pack, findFile(fileName)) 
    175             } 
    176           } catch { 
    177             case e: FileNotFoundException => 
    178           } 
    179         case _ => 
    180       } 
    181     } 
     145      parseLine1(pack, line) 
     146    } 
     147  } 
     148 
     149  def parseLine1(pack: Package, line: String) = line match { 
     150    case DeclareOption(option) => 
     151      pack.options += option 
     152    case DeclareOptionBeamer(option) => 
     153      pack.options += option 
     154    case RequirePackage(packList) => 
     155      pack.requiresPackages ++= packList.split(",").toList.map(_.trim()).filterNot(_.contains("<")) 
     156    case Def(cmd, args) => 
     157      pack.commands += cmd -> new Command(pack, cmd, DefArgCount.findAllIn(args).size) 
     158      if (cmd.startsWith("end")) { 
     159        val envName = cmd.substring(3) 
     160        for (startCmd <- pack.commands.get(envName)) { 
     161          pack.environments += envName -> new Environment(pack, envName, startCmd.argCount, startCmd.optionalArgs) 
     162        } 
     163      } 
     164    case NewCommand(cmd, args, optArg) => 
     165      val argCount = if (args == null) 0 else args.toInt 
     166      pack.commands += cmd -> new Command(pack, cmd, argCount, if (optArg == null) List() else List(optArg)) 
     167    case DeclareSymbol(cmd) => 
     168      pack.commands += cmd -> new Command(pack, cmd, 0, List()) 
     169    case NewEnvironment(cmd, args, optArg) => 
     170      val argCount = if (args == null) 0 else args.toInt 
     171      pack.environments += cmd -> new Environment(pack, cmd, argCount, if (optArg == null) List() else List(optArg)) 
     172    case _ => parseLine2(pack, line) 
     173  } 
     174 
     175  /** parseLine is splitted into parseLine1 and parseLine2 because of https://issues.scala-lang.org/browse/SI-1133 */ 
     176  def parseLine2(pack: Package, line: String) = line match { 
     177    case NewLength(len) => 
     178      pack.lengths += len 
     179    case NewCounter(counter) => 
     180      pack.counters += counter 
     181    case Input(fileName) => 
     182      try { 
     183        if (!processedFiles.contains(fileName)) { 
     184          processedFiles += fileName 
     185          //println("  input ." + fileName + ".") 
     186          parseFile(pack, findFile(fileName)) 
     187        } 
     188      } catch { 
     189        case e: FileNotFoundException => 
     190      } 
     191    case _ => 
    182192  } 
    183193