summaryrefslogtreecommitdiff
path: root/dice-lang/src/bjc/dicelang/v2/DoubleMatcher.java
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2017-02-11 08:44:28 -0500
committerbculkin2442 <bjculkin@mix.wvu.edu>2017-02-11 08:44:28 -0500
commit1cf218ba93396c7be7f4b3ee25d8008a41777273 (patch)
tree24fbb7a13c206c75e7c828862e04a5d0ae64cd5a /dice-lang/src/bjc/dicelang/v2/DoubleMatcher.java
parent6ca8c6764a8b8769a7a295c0479962a6588580a2 (diff)
Parse double and some dice
Diffstat (limited to 'dice-lang/src/bjc/dicelang/v2/DoubleMatcher.java')
-rw-r--r--dice-lang/src/bjc/dicelang/v2/DoubleMatcher.java58
1 files changed, 58 insertions, 0 deletions
diff --git a/dice-lang/src/bjc/dicelang/v2/DoubleMatcher.java b/dice-lang/src/bjc/dicelang/v2/DoubleMatcher.java
new file mode 100644
index 0000000..3c6f6c5
--- /dev/null
+++ b/dice-lang/src/bjc/dicelang/v2/DoubleMatcher.java
@@ -0,0 +1,58 @@
+package bjc.dicelang.v2;
+
+import java.util.regex.Pattern;
+
+/**
+ * Checks if a string would pass Double.parseDouble.
+ *
+ * Uses a regex from the javadoc for Double.valueOf()
+ */
+public class DoubleMatcher {
+ private static final String Digits =
+ "(\\p{Digit}+)";
+ private static final String HexDigits =
+ "(\\p{XDigit}+)";
+
+ // an exponent is 'e' or 'E' followed by an optionally
+ // signed decimal integer.
+ private static final String Exp =
+ "[eE][+-]?" + Digits;
+
+ private static final String fpRegex =
+ ("[\\x00-\\x20]*" +
+ // Optional leading "whitespace"
+ "[+-]?(" + // Optional sign character
+ "NaN|" + // "NaN" string
+ "Infinity|" + // "Infinity" string
+
+ // A decimal floating-point string representing a finite positive
+ // number without a leading sign has at most five basic pieces:
+ // Digits . Digits ExponentPart FloatTypeSuffix
+ //
+ // Since this method allows integer-only strings as input
+ // in addition to strings of floating-point literals, the
+ // two sub-patterns below are simplifications of the grammar
+ // productions from section 3.10.2 of
+ // The Java™ Language Specification.
+
+ // Digits ._opt Digits_opt ExponentPart_opt FloatTypeSuffix_opt
+ "((("+Digits+"(\\.)?("+Digits+"?)("+Exp+")?)|"+
+
+ // . Digits ExponentPart_opt FloatTypeSuffix_opt
+ "(\\.("+Digits+")("+Exp+")?)|"+
+
+ // Hexadecimal strings
+ "((" +
+ // 0[xX] HexDigits ._opt BinaryExponent FloatTypeSuffix_opt
+ "(0[xX]" + HexDigits + "(\\.)?)|" +
+
+ // 0[xX] HexDigits_opt . HexDigits BinaryExponent FloatTypeSuffix_opt
+ "(0[xX]" + HexDigits + "?(\\.)" + HexDigits + ")" +
+
+ ")[pP][+-]?" + Digits + "))" +
+ "[fFdD]?))" +
+ "[\\x00-\\x20]*");// Optional trailing "whitespace"
+
+ public static final Pattern floatingLiteral = Pattern.compile(fpRegex);
+
+}