]> sourceware.org Git - frysk.git/commitdiff
Introduce options FQ identifier parser
authorPetr Machata <pmachata@redhat.com>
Mon, 9 Jun 2008 14:33:16 +0000 (16:33 +0200)
committerPetr Machata <pmachata@redhat.com>
Mon, 9 Jun 2008 14:33:16 +0000 (16:33 +0200)
frysk-core/frysk/bindir/ftrace.java
frysk-core/frysk/expr/CExpr.g
frysk-core/frysk/expr/ChangeLog
frysk-core/frysk/expr/FQIdentParser.java

index 7bd544d96b76ba3b17f9579113449da88d9f8afe..86cb8fcf4dc70ec5183dbf06e9e20c448387a412 100644 (file)
@@ -149,7 +149,7 @@ class ftrace {
                                 RuleOptions options, Collection rules) {
 
                    try {
-                       FQIdentifier fqid = FQIdentParser.parseFQIdentifier(str);
+                       FQIdentifier fqid = FQIdentParser.parseFtraceIdentifier(str);
                        rules.add(new SymbolRule(addition, options, fqid));
                    }
                    catch (FQIdentParser.ExtraGarbageException exc) {
index f2c29966a44b9a1d23a2dd745b1e7556438565c5..3ea45b695c8e56155177fcdc519fe25e49b44f1e 100644 (file)
@@ -415,7 +415,8 @@ tokens
 }
 
 {
-    final FQIdentParser fqIdParser = new FQIdentParser(this);
+    final FQIdentParser fqIdParser
+        = new FQIdentParser(this, true, false, true);
 }
 
 AMPERSAND       : '&' ;
index b95cd1f079b1b82fe6f4324e19a1cc143b36c149..9b25bfc6355dcbf8522a3f68b9db6fe5a2319e70 100644 (file)
@@ -1,3 +1,8 @@
+2008-06-09  Petr Machata  <pmachata@redhat.com>
+
+       * FQIdentParser.java: Introduce parsing options.
+       (static parseFQIdentifier): Don't create the lexer at all.
+
 2008-06-09  Petr Machata  <pmachata@redhat.com>
 
        * CExpr.g: Cut FQ identifier parser out...
index ff279536a1c4066260c3acce80acb1b0878bbf0f..63035b9a1c8c65c667f51984fa3c938d5ad69ffa 100644 (file)
@@ -49,15 +49,43 @@ import antlr.MismatchedCharException;
 import antlr.RecognitionException;
 import antlr.Token;
 import antlr.TokenStreamException;
+import antlr.InputBuffer;
+import antlr.CharBuffer;
 
 public class FQIdentParser {
 
     private int i;
     private String fqinit;
     private final CharScanner scanner;
+    private final boolean allowDynamic;
+    private final boolean allowGlobs;
+    private final boolean expectMoreTokens;
+
+    /**
+     * @param allowDynamic Whether the [pid.tid#frame] portion of the
+     *        FQ syntax makes sense in given context.  For example it
+     *        doesn't for ftrace, but in general does for hpd.
+     *
+     * @param allowGlobs Whether globs should be allowed.  This
+     *        changes syntax of symbol portion of FQ identifier, which
+     *        becomes essentially unrestricted.  Note that is globs
+     *        are allowed, simple expressions as e.g. "a*b" are no
+     *        longer parsed as three tokens, but become one glob
+     *        symbol name.
+     *
+     * @param expectMoreTokens Whether whitespace terminates
+     *        lookahead.  When no more tokens are expected, it
+     *        doesn't.
+     */
+    FQIdentParser(CharScanner scanner,
+                 boolean allowDynamic,
+                 boolean allowGlobs,
+                 boolean expectMoreTokens) {
 
-    FQIdentParser(CharScanner scanner) {
        this.scanner = scanner;
+       this.allowDynamic = allowDynamic;
+       this.allowGlobs = allowGlobs;
+       this.expectMoreTokens = expectMoreTokens;
     }
 
     private char fqLA(int i) throws CharStreamException {
@@ -92,7 +120,8 @@ public class FQIdentParser {
        while (true) {
            c = fqLA(i++);
            matched.append(c);
-           if (Character.isWhitespace(c) || c == CharScanner.EOF_CHAR)
+           if ((expectMoreTokens && Character.isWhitespace(c))
+               || c == CharScanner.EOF_CHAR)
                throw new RecognitionException("Nonterminated " + context
                                               + " `" + matched
                                               + "' in fully qualified notation.");
@@ -108,9 +137,18 @@ public class FQIdentParser {
        return matched.toString();
     }
 
-    public Token parse(String initial)
+    /**
+     * @param initial Portion of the character stream that is part of
+     *        the identifier, but was already consumed by lexer.
+     */
+    public FQIdentToken parse(String initial)
         throws RecognitionException, CharStreamException, TokenStreamException
     {
+       if (allowGlobs)
+           // XXX to fool java into thinking that we use allowGlobs
+           // when we actually don't.
+           System.out.print("");
+
        fqinit = initial;
        i = 0;
 
@@ -147,18 +185,20 @@ public class FQIdentParser {
         // qualification (except the symbol name) is superfluous, but
         // does allow the identifier to be fully qualified anyway.
 
-       part = maybeParsePrefix('[', ']', "dynamic context");
-       if (part != null) {
-           matched += part;
-           Matcher m = Pattern.compile("\\[[0-9]+\\.[0-9]+#[0-9]+\\]").matcher(part);
-           if (!m.matches())
-               return null;
-
-           int hash = part.indexOf('#');
-           int dot = part.indexOf('.');
-           partProcessId = part.substring(1, dot);
-           partThreadId = part.substring(dot + 1, hash);
-           partFrameNum = part.substring(hash + 1, part.length() - 1);
+       if (allowDynamic) {
+           part = maybeParsePrefix('[', ']', "dynamic context");
+           if (part != null) {
+               matched += part;
+               Matcher m = Pattern.compile("\\[[0-9]+\\.[0-9]+#[0-9]+\\]").matcher(part);
+               if (!m.matches())
+                   return null;
+
+               int hash = part.indexOf('#');
+               int dot = part.indexOf('.');
+               partProcessId = part.substring(1, dot);
+               partThreadId = part.substring(dot + 1, hash);
+               partFrameNum = part.substring(hash + 1, part.length() - 1);
+           }
        }
 
        part = maybeParsePrefix('#', '#', "DSO part");
@@ -172,7 +212,8 @@ public class FQIdentParser {
        part = "";
         loop: while(true) {
             c = fqLA(i++);
-            if (Character.isWhitespace(c) || c == CharScanner.EOF_CHAR)
+            if ((expectMoreTokens && Character.isWhitespace(c))
+               || c == CharScanner.EOF_CHAR)
                 break;
 
             matched += c;
@@ -312,23 +353,29 @@ public class FQIdentParser {
         }
     }
 
-    public static FQIdentifier parseFQIdentifier(String str)
+    public static FQIdentifier
+    parseFQIdentifier(String str,
+                     boolean allowDynamic,
+                     boolean allowGlobs,
+                     boolean expectMoreTokens)
         throws ExtraGarbageException, InvalidTokenException
     {
-        CExprLexer lexer = new CExprLexer(new StringReader(str));
-
         try {
-           Token tok = lexer.fqIdParser.parse("");
-
-            if (!(tok instanceof FQIdentToken))
-                throw new InvalidTokenException(tok.getText());
-
-            FQIdentToken fqTok = (FQIdentToken)tok;
-
-            if ((tok = lexer.nextToken()).getType() != Token.EOF_TYPE)
-                throw new ExtraGarbageException(tok.getText());
-
-           return new FQIdentifier(fqTok);
+           InputBuffer ib = new CharBuffer(new StringReader(str));
+           CharScanner scanner = new CharScanner(ib) {
+                   public Token nextToken() throws TokenStreamException {
+                       return null;
+                   }
+               };
+           FQIdentParser parser
+               = new FQIdentParser(scanner, allowDynamic,
+                                   allowGlobs, expectMoreTokens);
+           FQIdentToken tok = parser.parse("");
+
+           if (scanner.LA(1) != CharScanner.EOF_CHAR)
+                throw new ExtraGarbageException(scanner.getText());
+
+           return new FQIdentifier(tok);
 
         } catch (TokenStreamException exc) {
             throw new InvalidTokenException(str);
@@ -338,4 +385,10 @@ public class FQIdentParser {
            throw new InvalidTokenException(str);
        }
     }
+
+    public static FQIdentifier parseFtraceIdentifier(String str)
+        throws ExtraGarbageException, InvalidTokenException
+    {
+       return parseFQIdentifier(str, false, true, false);
+    }
 }
This page took 0.033722 seconds and 5 git commands to generate.