Changeset 1091


Ignore:
Timestamp:
Jun 22, 2011, 10:54:08 PM (6 years ago)
Author:
stefan
Message:
 
Location:
trunk/tools
Files:
2 added
1 edited

Legend:

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

    r1079 r1091  
    11package jlatexeditor.tools
    22
    3 import java.io.{FileFilter, File}
    43import utils.ic.File._
     4import java.lang.Package
     5import java.io._
     6import collection.mutable.{HashMap, HashSet, MutableList}
    57
    68/**
     
    810 */
    911object PackageParser {
    10   val Def = ".*\\\\def\\\\(\\w+)\\{.*".r
    11   val DefParam = ".*\\\\def\\\\(\\w+)#(\\d+).*".r
     12  val Def = ".*\\\\def\\s*\\\\(\\w+)([#\\[\\]\\d]*)\\s*\\{.*".r
    1213  val NewCommand = ".*\\\\newcommand\\{\\\\(\\w+)\\}(?:\\[(\\d+)\\])?(?:\\[([^\\]]+)\\])?.*".r
    1314  val NewEnvironment = ".*\\\\newenvironment\\{(\\w+)\\}(?:\\[(\\d+)\\])?(?:\\[([^\\]]+)\\])?.*".r
    1415  val Input = ".*\\\\input\\{([^}]+)\\}".r
    1516
     17  val processedFiles = new HashSet[String]
     18
    1619  def main(args: Array[String]) {
    1720    //parseFile(new File("/usr/share/texmf-texlive/tex/latex/algorithms/algorithmic.sty"))
    18     parseFile(new File("/usr/share/texmf/tex/latex/pgf/frontendlayer/tikz.sty"))
    19 
    20     if (true) return
     21    parseFile(new Package("asdf"), new File("/usr/share/texmf/tex/latex/pgf/frontendlayer/tikz.sty"))
    2122
    2223    val texmfDirs = new File("/usr/share").listFiles(new FileFilter {
     
    2425    })
    2526
    26     texmfDirs.foreach(parse(_))
    27   }
     27    val packs = new MutableList[Package]
    2828
    29   def parse (dir: File) {
    30     for (file <- dir.listFiles()) {
    31       if (file.isDirectory) {
    32         parse(file)
    33       } else
    34       if (file.isFile && file.getName.endsWith(".sty")) {
    35         val packageName = file.getName.substring(0, file.getName.length() - 4)
    36         println(packageName)
     29    texmfDirs.foreach(dir => parse(packs, dir))
     30
     31    val commandName2command = new HashMap[String, MutableList[Command]]()
     32    for (pack <- packs; command <- pack.commands) {
     33      commandName2command.getOrElseUpdate(command.name, new MutableList[Command]) += command
     34    }
     35
     36    for (key <- commandName2command.keys) {
     37      println(key)
     38      for (command <- commandName2command(key)) {
     39        println("  " + command.name + " : " + command.argCount + " : " + command.pack.name)
    3740      }
    3841    }
    3942  }
    4043
    41   def parseFile (file: File) {
     44  def parse (packs: MutableList[Package], dir: File) {
     45    for (file <- dir.listFiles()) {
     46      if (file.isDirectory) {
     47        parse(packs, file)
     48      } else
     49      if (file.isFile && file.getName.endsWith(".sty")) {
     50        val packageName = file.getName.substring(0, file.getName.length() - 4)
     51        //println(packageName)
     52        val pack = new Package(packageName)
     53        packs += pack
     54        parseFile(pack, file);
     55      }
     56    }
     57  }
     58
     59  def parseFile (pack: Package, file: File) {
    4260    for(line <- file.readLines) {
    4361      line match {
    44         case Def(cmd) =>
    45           println(cmd)
    46         case DefParam(cmd, params) =>
    47           println(cmd + " with " + params + " arguments")
    48         case NewCommand(cmd, params, optParam) =>
    49           println(cmd + " with " + params + " arguments and " + optParam)
    50         case NewEnvironment(cmd, params, optParam) =>
    51           println("  environment " + cmd + " with " + params + " arguments and " + optParam)
     62        case Def(cmd, args) =>
     63          pack.commands += Command(pack, cmd, args.count(_ == '#'))
     64        case NewCommand(cmd, args, optArg) =>
     65          val argCount = if (args == null) 0 else args.toInt
     66          pack.commands += Command(pack, cmd, argCount, if (optArg == null) List() else List(optArg))
     67//          println(cmd + " with " + args + " arguments and " + optArgs)
     68        case NewEnvironment(cmd, args, optArg) =>
     69//          println("  environment " + cmd + " with " + args + " arguments and " + optArgs)
    5270        case Input(fileName) =>
    53           println("  input " + fileName)
     71          try {
     72            if (!processedFiles.contains(fileName)) {
     73              processedFiles += fileName
     74              //println("  input ." + fileName + ".")
     75              parseFile(pack, find(fileName))
     76            }
     77          } catch {
     78            case e: FileNotFoundException =>
     79          }
     80//          parseFile(pack, )
    5481        case _ =>
    5582      }
    5683    }
    5784  }
     85
     86  def find(fileName: String) = {
     87    val userDir = System.getProperty("user.dir");
     88    val process = Runtime.getRuntime.exec(Array(userDir + "/scripts/findTexFile", fileName))
     89    val r = new BufferedReader(new InputStreamReader(process.getInputStream))
     90    val s = r.readLine()
     91    if (s == null) throw new FileNotFoundException(fileName)
     92    else new File(s)
     93  }
     94
     95  case class Command(pack: Package, name: String, argCount: Int, optionalArgs: List[String] = List())
     96  case class Environment(pack: Package, name: String, argCount: Int, optionalArgs: List[String] = List())
     97  case class Package(name: String, commands: MutableList[Command] = new MutableList[Command],
     98                     environments: MutableList[Environment] = new MutableList[Environment])
    5899}
Note: See TracChangeset for help on using the changeset viewer.