Use java regex in a Scala application - scala.js

ScalaJs uses the javascript regexp library instead of the Java regexp library. Javascript regexp are a bit different, for instance they do not implement look-behind (?<=X).
How can I use the java regexp in a ScalaJs application?
(in order to have the look-behind feature)
ps: I am aware that sometimes it's possible to simulate look-behind in javascript regexp, as shown here, and I am currently doing this. However, this question is about how to use java regexp in ScalaJs.
pss: I am also aware that there is a regexp external javascript library with look-behind, xregexp. Again, this question is about how to use java regexp in ScalaJs.

Basically, you can't -- the Java Regexp, like most of the Java runtime environment, doesn't exist in the browser, so it doesn't exist for Scala.js. If you specifically need the Java Regex behavior, using one of those simulator libraries is probably your only option, at least for the time being.
As a rule of thumb, if something comes from the JRE, you should assume it does not exist in Scala.js unless someone has explicitly ported it. A growing amount has been ported, but it's still a fairly modest fraction of the total Java Runtime...

Related

Filter Eclipse CDT textual search results by program-structural features

I want to use Eclipse CDT to search for all occurrences of regular expresion, say foo(_bar_)*baz - in the bodies/bodies and declarations of functions/methods meeting a certain criterion. For the example let's make it all functions/methods named ignore_me (but within all classes and namespaces).
Is that possible somehow?
I'm not aware of a way to do this sort of search in Eclipse CDT.
Clang's AST matchers provide a rich domain-specific language for expressing queries like yours (and many other kinds), but using them requires writing code (in a clang plugin or standalone clang-based tool). It may be relatively straightforward to write a simple tool that allows you to write a query and searches a codebase for matches. I'm not sure whether such a tool is already available.

Looking for a standalone, command line, code generator script

I'm looking for a library or command line script that will allow me to create custom templates that I can generate from the command line. The ruby on rails scaffolding generator is almost identical to what I am trying to do. I would even prefer that it be written in Ruby (yet it cannot require Rails because I may not be using it on a Ruby application). What sorts of scripts like this are already available?
I've also been on the lookout for something like this -- haven't found what I hoped for. The two approaches I've used instead have been acceptable. But I'm still hoping to find the real thing.
obvious, but sed for simple use cases
for medium-complexity use cases, if you can assume some version of Python is present on the machine, string.Template in the standard library works well. You could write a small Python script that uses this function, and since it is Python, tests / looping etc. that might normally be provided by the template engine could pretty easily be handled in the Python code instead.
I've just discovered Mustache (see http://mustache.github.io/). Seems like a solid, purpose-built solution. From its web site, Mustache has implementations in Ruby, JavaScript, Python, Erlang, PHP, Perl, Objective-C, Java, .NET, Android, C++, Go, Lua, ActionScript, ColdFusion, Scala, Clojure, Fantom, CoffeeScript, D, and node.js. If those choices suit your environment, you could script or compile Mustache support to be a command line utility pretty easily.
UPDATE 15-OCT-2013
Now that I've used Mustache for a while -- it's a great tool, simple but powerful.
Try this one: https://gomplate.hairyhenderson.ca
Rich set of functions and just a stanalone binary (written in Go)
For simple use cases you can use envsubst fom the gettext GNU package.
Basically, it reads any (text) file from stdin, replaces all occurrences of $VARIABLE or ${VARIABLE} from environment and writes the result to stdout. Little more, nothing less.
Full documentation is here.
Pros:
It is a small binary, no python (or other runtime) required
It is very fast and has tiny memory footprint
Cons:
It does just that. No special interpolations and functions (like loops, conditionals etc.)
It doesn't support special bash-like "Parameter Expansion"
For more advanced use cases I recommend j2cli, a "standalone" Jinja2 templating engine you can install from pip as it is in Python. Code can be found here (though it looks like a little bit stale...) and documentation is there too.
Pros:
It's Jinja2 with all bells and whistles!
Cons:
You need full Python runtime to run it and PIP to install it.
Computing resources could not be negligible in constrained environments.

Xtext: grammar for language with significant/semantic whitespace

How can I use Xtext to parse languages with semantic whitespace? I'm trying to write a grammar for CoffeeScript and I can't find any good documentation on this.
Here's an example whitespace sensitive language in XText
AFAIK, you can't.
In case of parsing Python-like languages, you'd need the lexer to emit INDENT and DEDENT tokens. For that to happen, you'd need semantic predicates to be supported inside lexer rules (Xtext's terminal rules) that would first check if the current-position-in-line of the next character int the input equals 0 (the beginning of the line) and is a ' ' or '\t'.
But browsing through the documentation, I don't see this is supported by Xtext at the moment. Since Xtext 2.0, support has been added for semantic predicates in production rules (see: 6.2.8. Syntactic Predicates), but not in terminal rules.
The only way to do this with Xtext would be to let the lexer produce terminal spaces and line-breaks, but this would make an utter mess of your production rules.
If you want to parse such a language using Java (and a Java oriented parser generator) I'd recommend ANTLR, in which you can emit such INDENT and DEDENT tokens quite easily. But if you're keen on Eclipse integration, then I don't see how you'd be able to do this using Xtext, sorry.
Version 2.8 of Xtext comes with support for Whitespace-Aware Languages. This version ships with the "Home Automation Example" that you can use as a template.
For people interested in CoffeeScript, Adam Schmideg has an Eclipse plugin that uses XText.
For people interested in parsing Python-like DSL's in XText, Ralf Ebert's code for Todotext mentioned above is no longer available from Github but you can find it in the Eclipse test repository. See the original thread about this work and the Eclipse issue that was raised about it.
I have been playing with this code today and my conclusion is it no longer works in the current version of XText. When XText is used in Eclipse, I think it does "partial parsing". This is not compatible with the stateful lexer you need to process indentation sensative languages. So I suspect even if you patch the lexer, the Eclipse editor does not work. In the issue, it looks like Ralf proposed patches to address these issues, but looking into the XText source, these changes seem long gone? If I am wrong and someone can get it to work, I would be very interested?
There is a different implementation here but I cannot get that to work with the current version of XText either.
Instead I have switched to parboiled which does supports indentation based grammars out the box.

Which JVM functional languages are well IDE-supported? (IDE: IDEA, Netbeans, Eclipse or similar)

I have spent much time looking for a functional language that can seamlessly access Java legacy code (Weka code). Autocompletion of imported Java class methods would be excellent.
I will present my impressions, please correct me if I am wrong.
Yeti is very appealing to me because it is simple and ML-like, but I couldn't setup a good IDE for it. I don't know also if the language itself is already usable in real life.
Groovy, JRuby and Jython seem very mature, but are not really functional.
Scala is mature but is OO also and at least on Netbeans (and Eclipse if I remember well) it is buggy.
"OCaML-Java" is OO also and doesn't come ready to use (3 or 4 separate projects). I suppose that in IDEs it would be even less ready to use.
Jaskell appears to be dead.
Clojure seems mature and works really well in Intellij IDEA (La Clojure plugin), but I am not sure I want to write more than 2 parens per line while coding.
SISC and ABCL I didn't try. I think there is no IDE support.
CAL (or Quark ... Blah ...) language leads to a SAP site and after that my patience searching for functional JVM languages ended.
There are similar questions but not comprehensive. For instance:
Which Functional programming language offers best support in Eclipse?
Is some JVM functional language that is capable to call java classes directly missing?
If Yeti had IDE support as Clojure has (in IDEA), it would be perfect to me.
Thanks
EDIT-----------------------------------
I could use Clojure+Scala in Intellij IDEA.
Example of interoperability:
Scala code
package a
class ScalaMain {
def test = {
"testing!"
}
}
Clojure code
(import '(a ScalaMain)) ;'
(def scalainstance (new ScalaMain))
(print "printing: " (. scalainstance test))
Spectacular!
Scala + eclipse IDE (latest version) http://www.scala-ide.org/ seems to be getting better and better. Same for the IntellijIDEA plugin. There are lots of people/companies now developing with Scala... I don't think they all use Text Editors only.
I think that the most FP languages at the moment are Scala (multi-paradigm) and Clojure (pure functional) What is wrong with Scala being OO?
You can expect new languages comming such as Kotlin and Ceylon having functional features as well but they will also be OO, so I think your only choice now is between Scala and Clojure.
From your list you missing Fantom language. It also has Functional features, but I think it lacks IDE support (or atleast it is not updated to the latest versions)
UPDATE: There is now an eclipse-based IDE for the Fantom programming language that looks really nice. You can find it here: http://www.xored.com/products/f4/
Clojure is excellent when it comes to java interoperability + functional + having advantages from lisp. Though other IDE's support clojure , emacs works really well except that it takes time to learn and get used to emacs way of coding.
Clojure's interop story is fantastic. A major win with clojure is that its collections all implement the interfaces that come in the Java collections API, you don't have to adapt Clojure collections or copy them to Java collections to pass them to Java code as long as the Java code does not modify their contents. The reverse is also true, Java collections can be used with many of the Clojure functions that are useful like map reduce filter etc... I know somebody who ditched Scala for Clojure for this reason amongst others.
Its also easy to write Java interop code in Clojure that uses fewer parens than you would use in the equivalent Java code.
e.g.
blondie.callMe().callMe().any().any().time();
(-> blondie .callMe .callMe .any .any .time)
It takes some getting used to, but if you think about it, its not really all that different. Compare the following line of Java code v.s. Clojure, I think you'll understand both.
System.out.println("Hello World");
(println "Hello World")
Discounting the qualification of System.out and the additional semi-colon in the Java example, the only real difference is that the paren is now in front of the function call. Crazy talk!
It is different, but it may not be as bad as you think, and if you give it a try you might find compelling benefits in the language. If you want IDE support, check out LaClojure in IntelliJ or Enclojure from Netbeans. I know that Enclojure offers good completion support, which I think is really mostly only useful if you're dealing with lots of Java. Performing without a net in Clojure works fine.
Frege has now also a (still very basic) eclipse plugin.

Does Common Lisp have the fastest PCRE implementation?

A friend claimed that Common Lisp has the fastest Perl-compatible regular expression library of any language, including Perl itself, because with an optimizing JIT compiler like SBCL, CL-PPCRE can compile each particular regex down to native assembly whereas other implementations, include Perl's, must generate bytecode and interpret it. In practice, especially for the common case where we try to match the same regex against many inputs or long input, the compilation overhead is more than justified.
Unfortunately, I can't find any benchmarks on this, and I don't know enough to run my own, so I turn to the hive mind. Can anyone evaluate this claim?
I have no benchmarks of my own to share, but perhaps your friend was referring to results concerning the portable regular expression library CL-PPCRE. The current web page no longer speaks of benchmarks, but courtesy of the Wayback Machine we can see that it used to show benchmark results where CL-PPCRE outperformed Perl 2-to-1. Benchmarking is a tricky business (especially for moving targets), which might explain why the current page is silent on the matter.
PCRE library has a JIT compiler module now, and it has a good performance compared to other regexes: http://sljit.sourceforge.net/regex_perf.html or http://blog.rburchell.com/2011/12/why-i-avoid-qregexp-in-qt-4-and-so.html