我正在尝试实现一个合并某些源代码的不同版本的工具.给定相同源代码的两个版本,想法是解析它们,生成相应的抽象源树(AST),最后将它们合并到单个输出源中,保持语法一致性 - 词法分析器和解析器是ANTLR的问题:如何跳过多行注释.
我知道有一流的ParserRuleReturnScope
帮助...但getStop()
并getStart()
始终返回null :-(
这是一个片段,说明了我如何修改我的perser来打印规则:
parser grammar CodeTableParser;
options {
tokenVocab = CodeTableLexer;
backtrack = true;
output = AST;
}
@header {
package ch.bsource.ice.parsers;
}
@members {
private void log(ParserRuleReturnScope rule) {
System.out.println("Rule: " + rule.getClass().getName());
System.out.println(" getStart(): " + rule.getStart());
System.out.println(" getStop(): " + rule.getStop());
System.out.println(" getTree(): " + rule.getTree());
}
}
parse
: codeTabHeader codeTable endCodeTable eof { log(retval); }
;
codeTabHeader
: comment CodeTabHeader^ { log(retval); …
Run Code Online (Sandbox Code Playgroud) 给定以下需要对JSON进行序列化/反序列化的案例类...
import play.api.libs.json
import play.api.libs.functional.syntax._
trait MyTrait(s1: String, s2: String)
case class MyClass(s1: String, s2: String) extends MyTrait {
def this(t: MyTrait) = this(t.s1, t.s2)
}
object MyClass {
def apply(t: MyTrait) = new MyClass(t)
implicit val myClassJsonWrite = new Writes[MyClass] {
def writes(c: MyClass): JsValue = {
Json.obj(
"s1" -> c.s1,
"s2" -> c.s2
)
}
}
implicit val myClassJsonRead = (
(__ \ 's1).read[String] ~
(__ \ 's2).read[String]
)(MyClass.apply _)
}
Run Code Online (Sandbox Code Playgroud)
...我总是收到以下错误消息:
[error] /home/j3d/Projects/test/app/models/MyClass.scala:52: ambiguous reference to overloaded …
Run Code Online (Sandbox Code Playgroud) 我正在将我的Play 2应用程序转换为SPA,我正在试图弄清楚如何仍然使用SecureSocial进行身份验证.
我正在删除所有HTML模板(SecureSocial用于发送电子邮件的模板除外)并修改我的控制器以仅提供JSON响应.
是否可以阻止SecureSocial呈现HTML并让我的应用程序以JSON格式交换身份验证数据?是否有任何解释此主题的示例或教程?我已经谷歌搜索了几天,无法为像我这样的新手找到任何有用或至少可以理解的信息.
我想在MongoDB中更新JSON文档,如下所示:
{
"_id":{"$oid":"52dfc13ec20900c2093155cf"},
"email": "joe@domain.com",
"name": "joe",
"_version": 2
}
Run Code Online (Sandbox Code Playgroud)
...并希望在每次更新时创建这样的vermongo文档:
{
"_id { "_id":{"$oid":"52dfc13ec20900c2093155cf"}, "_version": 1},
"email": "joe@domain.com",
"name": "joe",
"_version": 1,
"_timestamp" : "2014-02-02T00:11:45.542"
}
Run Code Online (Sandbox Code Playgroud)
我尝试过这样的解决方案:
trait MyDao {
...
private val shadowCollection = ReactiveMongoPlugin.db.collection[JSONCollection](
collection.name + ".vermongo"
)
private def toVersioned(deleted: Boolean) = __.json.update(
(__ \ '_id).json.copyFrom((__ \ '_id \ '$oid).json.pickBranch) andThen
(__ \ '_id \ '_version).json.copyFrom((__ \ '_version).json.pick) andThen
// (__ \ '_version).json.put(if (deleted) JsString(s"deleted:$version") else JsNumber(version)) andThen
(__ \ '_timestamp).json.put(Json.toJson(LocalDateTime.now))
)
private def version(doc: JsValue, …
Run Code Online (Sandbox Code Playgroud) 鉴于以下字符串......
"localhost:9000/one/two/three"
Run Code Online (Sandbox Code Playgroud)
我希望在单词之后将其截断two
并获取
"localhost:9000/one/two"
Run Code Online (Sandbox Code Playgroud)
我已经实现了这些方法truncateBefore
并且truncateAfter
像这样:
def truncateBefore(s: String, p: String) = {
s.substring(s.indexOf(p) + p.length, s.length)
}
def truncateAfter(s: String, p: String) = {
s.substring(0, s.indexOf(p) + p.length)
}
Run Code Online (Sandbox Code Playgroud)
这些方法起作用并返回预期结果:
scala> truncateAfter("localhost:9000/one/two/three", "two")
res1: String = "localhost:9000/one/two"
scala> truncateBefore("localhost:9000/one/two/three", "two")
res2: String = "/three"
Run Code Online (Sandbox Code Playgroud)
在scala中有更好的方法吗?最好用正则表达式?
鉴于以下json ......
var body = "{ \"name\": \"test\", \"description\": \"test json\", \"website\": \"domain.com\" }"
Run Code Online (Sandbox Code Playgroud)
...除了值中的空格之外,我该如何删除所有空格?
我试过以下正则表达式......
var body = "{ \"name\": \"test\", \"description\": \"test json\", \"website\": \"domain.com\" }".replace(/\r?\n|\r/g, "").replace(/\s+/g, "")
Run Code Online (Sandbox Code Playgroud)
...但它也删除了值中的空格(即description
):
{"name":"test","description":"testjson","website":"domain.com"}
Run Code Online (Sandbox Code Playgroud)
我需要获得
{"name":"test","description":"test json","website":"domain.com"}
Run Code Online (Sandbox Code Playgroud)
TX.
给定一个返回Future
这样的方法......
def myMethod(name: String, count: Int, default: Boolean): Future[Unit] = {
...
}
Run Code Online (Sandbox Code Playgroud)
...我需要调用它N次,所以我已经定义了一个包含要传递的参数的元组列表:
val paramList = List(
("text1", 22, true),
("text2", 55, true),
("text3", 77, false)
)
Run Code Online (Sandbox Code Playgroud)
如何调用myMethod
带Future.traverse
?
Future.traverse(paramList)(myMethod _).tupled(/* how do I pass the current tuple here? */)
Run Code Online (Sandbox Code Playgroud) 以下代码段显示了如何将上载的文件直接保存到MongoDB:
object MyController extends Controller {
...
def saveImage = Action.async(fsBodyParser) { implicit request =>
val result = for { file <- request.body.files.head.ref
update <- {
fsService.update(
file.id,
Json.obj("metadata" -> Json.obj("category" -> "image"))
)
}
} yield update
result.map { _ =>
Created(success).withHeaders(LOCATION -> s"${localHost.baseUrl}${request.uri}")
}
}
private def fsBodyParser()(
implicit fsService: FsServiceComponent#FsService
): BodyParser[MultipartFormData[Future[MetaFile]]] = {
import BodyParsers.parse._
multipartFormData(Multipart.handleFilePart {
case Multipart.FileInfo(partName, filename, contentType) =>
fsService.iteratee(filename, contentType)
})
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码编译并正常工作到Play 2.3.x ...如果我尝试使用Play 2.4.x编译它,我总是会收到以下错误消息:
[error] /home/j3d/Projects/test/app/controllers/MyController.scala:71: not found: value …
Run Code Online (Sandbox Code Playgroud) 给定一个代表信用卡号的字符串......
val creditCardNo = "1111222233334444"
Run Code Online (Sandbox Code Playgroud)
...如何用前缀12个字符*
?
val maskedCreditCardNo = "************4444"
Run Code Online (Sandbox Code Playgroud) 我需要在加载配置文件之后但在应用程序实际启动之前读取一些配置值.
在Play 2.3.x中,我曾经使用覆盖GlobalSettings.onLoadConfig
,在Play 2.4.x中已弃用.官方文件说应该使用GuiceApplicationBuilder.loadConfig
.
再次,文档有点差,我无法找到更多的细节或示例......所以任何帮助将非常感激.
scala ×7
json ×3
regex ×2
antlr ×1
file-upload ×1
future ×1
java ×1
javascript ×1
lexer ×1
mongodb ×1
parsing ×1
securesocial ×1