Search in sources :

Example 36 with MatchResult

use of java.util.regex.MatchResult in project robovm by robovm.

the class ScannerTest method test_findInLine_LString.

public void test_findInLine_LString() {
    Scanner s = new Scanner("");
    String result = s.findInLine("^");
    assertEquals("", result);
    MatchResult matchResult = s.match();
    assertEquals(0, matchResult.start());
    assertEquals(0, matchResult.end());
    result = s.findInLine("$");
    assertEquals("", result);
    matchResult = s.match();
    assertEquals(0, matchResult.start());
    assertEquals(0, matchResult.end());
    // When we use the operation of findInLine(Pattern), the match region
    // should not span the line separator.
    s = new Scanner("aa\nb.b");
    result = s.findInLine("a\nb*");
    assertNull(result);
    s = new Scanner("aa\nbb.b");
    result = s.findInLine("\\.");
    assertNull(result);
    s = new Scanner("abcd1234test\n");
    result = s.findInLine("\\p{Lower}+");
    assertEquals("abcd", result);
    matchResult = s.match();
    assertEquals(0, matchResult.start());
    assertEquals(4, matchResult.end());
    result = s.findInLine("\\p{Digit}{5}");
    assertNull(result);
    try {
        matchResult = s.match();
        fail();
    } catch (IllegalStateException expected) {
    }
    assertEquals(0, matchResult.start());
    assertEquals(4, matchResult.end());
    result = s.findInLine("\\p{Lower}+");
    assertEquals("test", result);
    matchResult = s.match();
    assertEquals(8, matchResult.start());
    assertEquals(12, matchResult.end());
    char[] chars = new char[2048];
    Arrays.fill(chars, 'a');
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append(chars);
    stringBuilder.append("1234");
    s = new Scanner(stringBuilder.toString());
    result = s.findInLine("\\p{Digit}+");
    assertEquals("1234", result);
    matchResult = s.match();
    assertEquals(2048, matchResult.start());
    assertEquals(2052, matchResult.end());
    s = new Scanner("test1234\n1234 test");
    result = s.findInLine("test");
    assertEquals("test", result);
    matchResult = s.match();
    assertEquals(0, matchResult.start());
    assertEquals(4, matchResult.end());
    int number = s.nextInt();
    assertEquals(1234, number);
    matchResult = s.match();
    assertEquals(4, matchResult.start());
    assertEquals(8, matchResult.end());
    result = s.next();
    assertEquals("1234", result);
    matchResult = s.match();
    assertEquals(9, matchResult.start());
    assertEquals(13, matchResult.end());
    result = s.findInLine("test");
    assertEquals("test", result);
    matchResult = s.match();
    assertEquals(14, matchResult.start());
    assertEquals(18, matchResult.end());
    s = new Scanner("test…\ntest");
    result = s.findInLine("est");
    assertEquals("est", result);
    result = s.findInLine("est");
    assertEquals("est", result);
    s = new Scanner("test\ntest");
    result = s.findInLine("est");
    assertEquals("est", result);
    result = s.findInLine("est");
    assertEquals("est", result);
    s = new Scanner("test\n123\ntest");
    result = s.findInLine("est");
    assertEquals("est", result);
    result = s.findInLine("est");
}
Also used : Scanner(java.util.Scanner) MatchResult(java.util.regex.MatchResult)

Example 37 with MatchResult

use of java.util.regex.MatchResult in project robovm by robovm.

the class ScannerTest method test_nextLine.

/**
     * @tests java.util.Scanner#nextLine()
     */
public void test_nextLine() {
    s = new Scanner("");
    s.close();
    try {
        s.nextLine();
        fail();
    } catch (IllegalStateException expected) {
    }
    s = new Scanner("test\r\ntest");
    String result = s.nextLine();
    assertEquals("test", result);
    MatchResult matchResult = s.match();
    assertEquals(0, matchResult.start());
    assertEquals(6, matchResult.end());
    s = new Scanner("…");
    result = s.nextLine();
    assertEquals("", result);
    matchResult = s.match();
    assertEquals(0, matchResult.start());
    assertEquals(1, matchResult.end());
    s = new Scanner("
");
    result = s.nextLine();
    assertEquals("", result);
    matchResult = s.match();
    assertEquals(0, matchResult.start());
    assertEquals(1, matchResult.end());
    s = new Scanner("
");
    result = s.nextLine();
    assertEquals("", result);
    matchResult = s.match();
    assertEquals(0, matchResult.start());
    assertEquals(1, matchResult.end());
    s = new Scanner("");
    try {
        result = s.nextLine();
        fail();
    } catch (NoSuchElementException expected) {
    }
    try {
        s.match();
        fail();
    } catch (IllegalStateException expected) {
    }
    s = new Scanner("Ttest");
    result = s.nextLine();
    assertEquals("Ttest", result);
    matchResult = s.match();
    assertEquals(0, matchResult.start());
    assertEquals(5, matchResult.end());
    s = new Scanner("\r\n");
    result = s.nextLine();
    assertEquals("", result);
    matchResult = s.match();
    assertEquals(0, matchResult.start());
    assertEquals(2, matchResult.end());
    char[] chars = new char[1024];
    Arrays.fill(chars, 'a');
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append(chars);
    chars = new char[] { '+', '-' };
    stringBuilder.append(chars);
    stringBuilder.append("
");
    s = new Scanner(stringBuilder.toString());
    result = s.nextLine();
    assertEquals(stringBuilder.toString().substring(0, 1026), result);
    matchResult = s.match();
    assertEquals(0, matchResult.start());
    assertEquals(1027, matchResult.end());
    chars = new char[1023];
    Arrays.fill(chars, 'a');
    stringBuilder = new StringBuilder();
    stringBuilder.append(chars);
    stringBuilder.append("\r\n");
    s = new Scanner(stringBuilder.toString());
    result = s.nextLine();
    assertEquals(stringBuilder.toString().substring(0, 1023), result);
    matchResult = s.match();
    assertEquals(0, matchResult.start());
    assertEquals(1025, matchResult.end());
    s = new Scanner("  ");
    result = s.nextLine();
    assertEquals("  ", result);
    s = new Scanner("test\n\n\n");
    result = s.nextLine();
    assertEquals("test", result);
    matchResult = s.match();
    assertEquals(0, matchResult.start());
    assertEquals(5, matchResult.end());
    result = s.nextLine();
    matchResult = s.match();
    assertEquals(5, matchResult.start());
    assertEquals(6, matchResult.end());
    s = new Scanner("\n\n\n");
    result = s.nextLine();
    assertEquals("", result);
    matchResult = s.match();
    assertEquals(0, matchResult.start());
    assertEquals(1, matchResult.end());
    result = s.nextLine();
    matchResult = s.match();
    assertEquals(1, matchResult.start());
    assertEquals(2, matchResult.end());
    s = new Scanner("123 test\n   ");
    int value = s.nextInt();
    assertEquals(123, value);
    result = s.nextLine();
    assertEquals(" test", result);
    s = new Scanner("test\n ");
    result = s.nextLine();
    assertEquals("test", result);
    // Regression test for Harmony-4774
    class CountReadable implements Readable {

        int counter = 0;

        public int read(CharBuffer charBuffer) throws IOException {
            counter++;
            charBuffer.append("hello\n");
            return 6;
        }
    }
    CountReadable cr = new CountReadable();
    s = new Scanner(cr);
    result = s.nextLine();
    // We expect read() to be called only once, otherwise we see the problem
    // when reading from System.in described in Harmony-4774
    assertEquals(1, cr.counter);
    assertEquals("hello", result);
}
Also used : Scanner(java.util.Scanner) CharBuffer(java.nio.CharBuffer) MatchResult(java.util.regex.MatchResult) NoSuchElementException(java.util.NoSuchElementException)

Example 38 with MatchResult

use of java.util.regex.MatchResult in project robovm by robovm.

the class ScannerTest method test_hasNextLine.

/**
     * @tests java.util.Scanner#hasNextLine()
     */
public void test_hasNextLine() {
    s = new Scanner("");
    s.close();
    try {
        s.hasNextLine();
        fail();
    } catch (IllegalStateException expected) {
    }
    s = new Scanner("test\r\ntest");
    boolean result = s.hasNextLine();
    assertTrue(result);
    MatchResult matchResult = s.match();
    assertEquals(0, matchResult.start());
    assertEquals(6, matchResult.end());
    s = new Scanner("…");
    result = s.hasNextLine();
    assertTrue(result);
    matchResult = s.match();
    assertEquals(0, matchResult.start());
    assertEquals(1, matchResult.end());
    s = new Scanner("
");
    result = s.hasNextLine();
    assertTrue(result);
    matchResult = s.match();
    assertEquals(0, matchResult.start());
    assertEquals(1, matchResult.end());
    s = new Scanner("
");
    result = s.hasNextLine();
    assertTrue(result);
    matchResult = s.match();
    assertEquals(0, matchResult.start());
    assertEquals(1, matchResult.end());
    s = new Scanner("test\n");
    assertTrue(s.hasNextLine());
    matchResult = s.match();
    assertEquals(0, matchResult.start());
    assertEquals(5, matchResult.end());
    char[] chars = new char[2048];
    Arrays.fill(chars, 'a');
    StringBuilder stringBuilder = new StringBuilder();
    stringBuilder.append(chars);
    s = new Scanner(stringBuilder.toString());
    result = s.hasNextLine();
    assertTrue(result);
    matchResult = s.match();
    assertEquals(0, matchResult.start());
    assertEquals(2048, matchResult.end());
    s = new Scanner("\n\n\n");
    assertTrue(s.hasNextLine());
    matchResult = s.match();
    assertEquals(0, matchResult.start());
    assertEquals(1, matchResult.end());
    // The scanner will not advance any input.
    assertTrue(s.hasNextLine());
    matchResult = s.match();
    assertEquals(0, matchResult.start());
    assertEquals(1, matchResult.end());
}
Also used : Scanner(java.util.Scanner) MatchResult(java.util.regex.MatchResult)

Example 39 with MatchResult

use of java.util.regex.MatchResult in project processing by processing.

the class PdePreprocessor method parseSketchSize.

/**
   * Parse a chunk of code and extract the size() command and its contents.
   * Also goes after fullScreen(), smooth(), and noSmooth().
   * @param code The code from the main tab in the sketch
   * @param fussy true if it should show an error message if bad size()
   * @return null if there was an error, otherwise an array (might contain some/all nulls)
   */
public static SurfaceInfo parseSketchSize(String code, boolean fussy) throws SketchException {
    // This matches against any uses of the size() function, whether numbers
    // or variables or whatever. This way, no warning is shown if size() isn't
    // actually used in the applet, which is the case especially for anyone
    // who is cutting/pasting from the reference.
    //    String scrubbed = scrubComments(sketch.getCode(0).getProgram());
    //    String[] matches = PApplet.match(scrubbed, SIZE_REGEX);
    //    String[] matches = PApplet.match(scrubComments(code), SIZE_REGEX);
    /*
   1. no size() or fullScreen() method at all
      will use the non-overridden settings() method in PApplet
   2. size() or fullScreen() found inside setup() (static mode sketch or otherwise)
      make sure that it uses numbers (or displayWidth/Height), copy into settings
   3. size() or fullScreen() already in settings()
      don't mess with the sketch, don't insert any defaults

   really only need to deal with situation #2.. nothing to be done for 1 and 3
   */
    // if static mode sketch, all we need is regex
    // easy proxy for static in this case is whether [^\s]void\s is present
    String uncommented = scrubComments(code);
    Mode mode = parseMode(uncommented);
    String searchArea = null;
    switch(mode) {
        case JAVA:
            // it's up to the user
            searchArea = null;
            break;
        case ACTIVE:
            // active mode, limit scope to setup
            // Find setup() in global scope
            MatchResult setupMatch = findInCurrentScope(VOID_SETUP_REGEX, uncommented);
            if (setupMatch != null) {
                int start = uncommented.indexOf("{", setupMatch.end());
                if (start >= 0) {
                    // Find a closing brace
                    MatchResult match = findInCurrentScope(CLOSING_BRACE, uncommented, start);
                    if (match != null) {
                        searchArea = uncommented.substring(start + 1, match.end() - 1);
                    } else {
                        throw new SketchException("Found a { that's missing a matching }", false);
                    }
                }
            }
            break;
        case STATIC:
            // static mode, look everywhere
            searchArea = uncommented;
            break;
    }
    if (searchArea == null) {
        return new SurfaceInfo();
    }
    StringList extraStatements = new StringList();
    // First look for noSmooth() or smooth(N) so we can hoist it into settings.
    String[] smoothContents = matchMethod("smooth", searchArea);
    if (smoothContents != null) {
        extraStatements.append(smoothContents[0]);
    }
    String[] noContents = matchMethod("noSmooth", searchArea);
    if (noContents != null) {
        if (extraStatements.size() != 0) {
            throw new SketchException("smooth() and noSmooth() cannot be used in the same sketch");
        } else {
            extraStatements.append(noContents[0]);
        }
    }
    String[] pixelDensityContents = matchMethod("pixelDensity", searchArea);
    if (pixelDensityContents != null) {
        extraStatements.append(pixelDensityContents[0]);
    } else {
        pixelDensityContents = matchDensityMess(searchArea);
        if (pixelDensityContents != null) {
            extraStatements.append(pixelDensityContents[0]);
        }
    }
    String[] sizeContents = matchMethod("size", searchArea);
    String[] fullContents = matchMethod("fullScreen", searchArea);
    // throw a confusing state exception error that one "can't be used here".
    if (sizeContents != null && fullContents != null) {
        throw new SketchException("size() and fullScreen() cannot be used in the same sketch", false);
    }
    //String[] contents = PApplet.match(searchArea, SIZE_CONTENTS_REGEX);
    if (sizeContents != null) {
        StringList args = breakCommas(sizeContents[1]);
        SurfaceInfo info = new SurfaceInfo();
        //      info.statement = sizeContents[0];
        info.addStatement(sizeContents[0]);
        info.width = args.get(0).trim();
        info.height = args.get(1).trim();
        info.renderer = (args.size() >= 3) ? args.get(2).trim() : null;
        info.path = (args.size() >= 4) ? args.get(3).trim() : null;
        if (info.hasOldSyntax()) {
            //        return null;
            throw new SketchException("Please update your code to continue.", false);
        }
        if (info.hasBadSize() && fussy) {
            // found a reference to size, but it didn't seem to contain numbers
            final String message = "The size of this sketch could not be determined from your code.\n" + "Use only numbers (not variables) for the size() command.\n" + "Read the size() reference for more details.";
            Messages.showWarning("Could not find sketch size", message, null);
            //        return null;
            throw new SketchException("Please fix the size() line to continue.", false);
        }
        info.addStatements(extraStatements);
        info.checkEmpty();
        return info;
    //return new String[] { contents[0], width, height, renderer, path };
    }
    //contents = PApplet.match(searchArea, FULL_SCREEN_CONTENTS_REGEX);
    if (fullContents != null) {
        SurfaceInfo info = new SurfaceInfo();
        //      info.statement = fullContents[0];
        info.addStatement(fullContents[0]);
        StringList args = breakCommas(fullContents[1]);
        if (args.size() > 0) {
            // might have no args
            String args0 = args.get(0).trim();
            if (args.size() == 1) {
                // could be either fullScreen(1) or fullScreen(P2D), figure out which
                if (args0.equals("SPAN") || PApplet.parseInt(args0, -1) != -1) {
                    // it's the display parameter, not the renderer
                    info.display = args0;
                } else {
                    info.renderer = args0;
                }
            } else if (args.size() == 2) {
                info.renderer = args0;
                info.display = args.get(1).trim();
            } else {
                throw new SketchException("That's too many parameters for fullScreen()");
            }
        }
        info.width = "displayWidth";
        info.height = "displayHeight";
        //      if (extraStatements.size() != 0) {
        //        info.statement += extraStatements.join(" ");
        //      }
        info.addStatements(extraStatements);
        info.checkEmpty();
        return info;
    }
    // need to pull out the noSmooth() and smooth(N) methods.
    if (extraStatements.size() != 0) {
        SurfaceInfo info = new SurfaceInfo();
        //      info.statement = extraStatements.join(" ");
        info.addStatements(extraStatements);
        return info;
    }
    //return new String[] { null, null, null, null, null };
    return new SurfaceInfo();
}
Also used : SketchException(processing.app.SketchException) StringList(processing.data.StringList) MatchResult(java.util.regex.MatchResult)

Example 40 with MatchResult

use of java.util.regex.MatchResult in project processing by processing.

the class PdePreprocessor method matchMethod.

/**
   * Looks for the specified method in the base scope of the search area.
   */
protected static String[] matchMethod(String methodName, String searchArea) {
    final String left = "(?:^|\\s|;)";
    // doesn't match empty pairs of parens
    //final String right = "\\s*\\(([^\\)]+)\\)\\s*;";
    final String right = "\\s*\\(([^\\)]*)\\)\\s*;";
    String regexp = left + methodName + right;
    Pattern p = matchPatterns.get(regexp);
    if (p == null) {
        p = Pattern.compile(regexp, Pattern.MULTILINE | Pattern.DOTALL);
        matchPatterns.put(regexp, p);
    }
    MatchResult match = findInCurrentScope(p, searchArea);
    if (match != null) {
        int count = match.groupCount() + 1;
        String[] groups = new String[count];
        for (int i = 0; i < count; i++) {
            groups[i] = match.group(i);
        }
        return groups;
    }
    return null;
}
Also used : Pattern(java.util.regex.Pattern) MatchResult(java.util.regex.MatchResult)

Aggregations

MatchResult (java.util.regex.MatchResult)62 Matcher (java.util.regex.Matcher)26 Pattern (java.util.regex.Pattern)16 Scanner (java.util.Scanner)11 Point (java.awt.Point)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 IOException (java.io.IOException)3 NoSuchElementException (java.util.NoSuchElementException)3 XMLStreamException (javax.xml.stream.XMLStreamException)3 MatcherState (com.github.anba.es6draft.regexp.MatcherState)2 ArrayObject (com.github.anba.es6draft.runtime.types.builtins.ArrayObject)2 InputStream (java.io.InputStream)2 Fault (org.apache.cxf.interceptor.Fault)2 CachedOutputStream (org.apache.cxf.io.CachedOutputStream)2 IterableMatchResult (com.github.anba.es6draft.regexp.IterableMatchResult)1 MatcherResult (com.github.anba.es6draft.regexp.MatcherResult)1 ScriptObject (com.github.anba.es6draft.runtime.types.ScriptObject)1 OrdinaryObject (com.github.anba.es6draft.runtime.types.builtins.OrdinaryObject)1 ImmutableMap (com.google.common.collect.ImmutableMap)1