diff dwtx/jface/text/rules/WhitespaceRule.d @ 129:eb30df5ca28b

Added JFace Text sources
author Frank Benoit <benoit@tionex.de>
date Sat, 23 Aug 2008 19:10:48 +0200
parents
children c4fb132a086c
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/dwtx/jface/text/rules/WhitespaceRule.d	Sat Aug 23 19:10:48 2008 +0200
@@ -0,0 +1,62 @@
+/*******************************************************************************
+ * Copyright (c) 2000, 2006 IBM Corporation and others.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     IBM Corporation - initial API and implementation
+ * Port to the D programming language:
+ *     Frank Benoit <benoit@tionex.de>
+ *******************************************************************************/
+
+module dwtx.jface.text.rules.WhitespaceRule;
+
+import dwt.dwthelper.utils;
+
+
+import dwtx.core.runtime.Assert;
+
+
+/**
+ * An implementation of <code>IRule</code> capable of detecting whitespace.
+ * A whitespace rule uses a whitespace detector in order to find out which
+ * characters are whitespace characters.
+ *
+ * @see IWhitespaceDetector
+ */
+public class WhitespaceRule : IRule {
+
+    /** The whitespace detector used by this rule */
+    protected IWhitespaceDetector fDetector;
+
+    /**
+     * Creates a rule which, with the help of an
+     * whitespace detector, will return a whitespace
+     * token when a whitespace is detected.
+     *
+     * @param detector the rule's whitespace detector, may not be <code>null</code>
+     */
+    public WhitespaceRule(IWhitespaceDetector detector) {
+        Assert.isNotNull(detector);
+        fDetector= detector;
+    }
+
+    /*
+     * @see IRule#evaluate(ICharacterScanner)
+     */
+    public IToken evaluate(ICharacterScanner scanner) {
+        int c= scanner.read();
+        if (fDetector.isWhitespace((char) c)) {
+            do {
+                c= scanner.read();
+            } while (fDetector.isWhitespace((char) c));
+            scanner.unread();
+            return Token.WHITESPACE;
+        }
+
+        scanner.unread();
+        return Token.UNDEFINED;
+    }
+}