Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scala's compiler lexer

Tags:

scala

I'm trying to get a list of tokens (I'm most interested in keywords) and their positions for a given scala source file.

I think there is a lexer utility inside scala compiler, but I can't find it. Can you point me into the right direction?

like image 510
Rogach Avatar asked Jan 24 '26 21:01

Rogach


2 Answers

A simple lexer for a Scala-like language is provided in a standard library.

A small utility program which tokenizes Scala source using the same lexer as compiler does lives here

like image 112
Rafał Rawicki Avatar answered Jan 26 '26 14:01

Rafał Rawicki


Scalariform has an accurate Scala lexer you can use:

import scalariform.lexer._
val tokens = ScalaLexer.rawTokenise("class A", forgiveErrors = true)
val keywords = tokens.find(_.tokenType.isKeyword)
val comments = tokens.find(_.tokenType.isComment)
like image 44
Matt R Avatar answered Jan 26 '26 13:01

Matt R