Permalink
Browse files

more playing with syntax

- angle brackets for types
- needed round brackets for expressions in types in Bynar
  • Loading branch information...
stefanbohne committed May 26, 2017
1 parent 4c584fa commit 7ff28c23ad77db672dbeb8e9cd9e35eac659cd01
@@ -0,0 +1 @@
/VersaillesLangIdeModule.xtend
@@ -1,2 +1,2 @@
eclipse.preferences.version=1
encoding/<project>=windows-1252
encoding/<project>=UTF-8
@@ -29,7 +29,7 @@ Export-Package: org.bynar.versailles,
org.bynar.versailles.xtext.validation,
org.bynar.versailles.xtext.versaillesLang,
org.bynar.versailles.xtext.versaillesLang.impl,
org.bynar.versailles.xtext.versaillesLang.util
org.bynar.versailles.xtext.versaillesLang.util,
org.bynar.versailles.xtext.formatting2
Import-Package: org.apache.log4j
Bundle-ClassPath: lib/org.bynar.versailles.jar,
.
Bundle-ClassPath: lib/org.bynar.versailles.jar,.
@@ -198,7 +198,7 @@ class Converter {
it match {
case it: JanusClass =>
it.getOp match {
case "->" => v.Irreversible().putAnnotation(source, it)
case "->" | "" | "\\leftarrow" | "\\to" => v.Irreversible().putAnnotation(source, it)
case "<-" => v.ReverseIrreversible().putAnnotation(source, it)
case "<->" => v.Inverse().putAnnotation(source, it)
case ">->" => v.SemiInverse().putAnnotation(source, it)
@@ -42,6 +42,11 @@ Workflow {
validator = {
// composedCheck = "org.eclipse.xtext.validation.NamesAreUniqueValidator"
}
formatter = {
generateStub = true
}
}
}
}
@@ -14,11 +14,15 @@ Sep hidden (WS, ML_COMMENT, SL_COMMENT):
Name: ID_LOWER | "pass" | "fail" | "let" | "forget" | "remember" | "def" | "letdef" | "type" | "lettype" |
"if" | "else" | "fi" | "case" | "match" | "in" | "from" | "loop" | "until" | "do" |
"module" | "for" | "where" | "abstract" | ID_BACKTICK | TypeName
"module" | "for" | "where" | "abstract" | "tuple" | ID_BACKTICK | TypeName
;
TypeName: ID_UPPER | ID_FORWARDTICK;
TypeName: ID_UPPER | ID_BACKTICK;
NamePath: steps+=Name ("." steps+=Name)*;
terminal SYM_LANGLE: "〈" | "<." | "\\langle";
terminal SYM_RANGLE: "〉" | ".>" | "\\rangle";
terminal SYM_LEFTARROW: "→" | "->" | "\\leftarrow" | "\\to";
Statement:
{PassStmt} "pass"
@@ -31,11 +35,11 @@ Statement:
| {IfStmt} "if" condition=Expression "then" then=Statements (=> "else" else=Statements)? "fi" (assertion=Expression)?
| {LoopStmt} ("from" assertion=Expression)? ("do" do=Statements)? ("loop" loop=Statements)? "until" condition=Expression
| {CaseStmt} "case" case=LambdaExpr
| {DefStmt} ("def"|let?="letdef") name=Name (typeArguments=TupleTypeExpr)? (arguments+=TupleExpr)* (title=STRING)? (description=InterpolatedString)?
( janusClass=JanusClassExpression name2=Name? results=TupleExpr (title2=STRING)? (description2=InterpolatedString)? ("{" statements=Statements "}")?
| {DefStmt} ("def"|let?="letdef") name=Name (SYM_LANGLE typeArguments=TupleTypeExpr SYM_RANGLE)? ("(" arguments+=TupleExpr ")")* (title=STRING)? (description=InterpolatedString)?
( janusClass=JanusClassExpression name2=Name? "(" results=TupleExpr ")" (title2=STRING)? (description2=InterpolatedString)? ("{" statements=Statements "}")?
| ":" valueType=TypeExpression ("=" value=Expression)?)
| {TypeStmt} ("type"|let?="lettype") name=Name typeArguments=TupleTypeExpr? (arguments+=TupleExpr)* (title=STRING)? (description=InterpolatedString)?
(":" kind=TypeExpression)? ("=" type=TypeExpression)?
| {TypeStmt} ("type"|let?="lettype") name=Name (SYM_LANGLE typeArguments=TupleTypeExpr SYM_RANGLE)? ("(" arguments+=TupleExpr ")")* (title=STRING)? (description=InterpolatedString)?
("::" kind=TypeExpression)? ("=" type=TypeExpression)?
| {ModuleStmt} "module" path=NamePath (title=STRING)? (description=InterpolatedString)? "{" statements=Statements "}"
;
Statements:
@@ -45,7 +49,7 @@ Statements:
Expression:
ExpressionLambda
;
JanusClass: {JanusClass} op=("->" | "<-" | "<->" | ">->" | "<-<" | ">-<" | "<>-<>");
JanusClass: {JanusClass} op=(SYM_LEFTARROW | "<-" | "<->" | ">->" | "<-<" | ">-<" | "<>-<>");
JanusClassExpression returns JanusClassExpression:
JanusClass
| {GenericJanusClass} ">-" variable=TypeVariable "->"
@@ -55,7 +59,7 @@ LambdaExpr:
pattern=ExpressionIf janusClass=JanusClass body=Expression
;
ExpressionLambda returns Expression:
ExpressionIf ({LambdaExpr.pattern=current} janusClass=JanusClass body=Expression)*
ExpressionIf ({LambdaExpr.pattern=current} => janusClass=JanusClass body=Expression)*
;
ExpressionIf returns Expression:
ExpressionWhere ({IfExpr.then=current} => "if" condition=Expression "else" else=ExpressionWhere)*
@@ -76,10 +80,7 @@ ExpressionConj returns Expression:
;
ComparisionOp returns Operator: {Operator} op=("==" | "<" | ">" | "<=" | ">=" | "!=" | "in");
ExpressionComp returns Expression:
ExpressionTyped ({BinaryExpr.left=current} op=ComparisionOp => right=ExpressionTyped)*
;
ExpressionTyped returns Expression:
ExpressionConcat ({TypedExpr.base=current} => ":" type=TypeExpression)?
ExpressionConcat ({BinaryExpr.left=current} op=ComparisionOp right=ExpressionConcat)?
;
ConcatOp returns Operator: {Operator} op=("++");
ExpressionConcat returns Expression:
@@ -91,16 +92,19 @@ ExpressionAdd returns Expression:
;
MulOp returns Operator: {Operator} op=("*" | "/" | "%");
ExpressionMul returns Expression:
ExpressionUn ({BinaryExpr.left=current} => op=MulOp right=ExpressionUn)*
ExpressionTyped ({BinaryExpr.left=current} => op=MulOp right=ExpressionTyped)*
;
ExpressionTyped returns Expression:
ExpressionUn ({TypedExpr.base=current} => ":" type=TypeExpression)?
;
UnaryOp returns Operator: {Operator} op=("~" | "-" | "!");
ExpressionUn returns Expression:
{UnaryExpr} => op=UnaryOp expr=ExpressionUn
| ExpressionAppl
;
ExpressionAppl returns Expression:
ExpressionSimple ( {ApplicationExpr.function=current} => argument=TupleExpr
| {TypeApplicationExpr.function=current} => argument=TupleTypeExpr
ExpressionSimple ( {ApplicationExpr.function=current} => "(" argument=TupleExpr ")"
| {TypeApplicationExpr.function=current} => SYM_LANGLE argument=TupleTypeExpr SYM_RANGLE
| {MemberAccessExpr.base=current} => "." memberName=Name)*
;
ExpressionSimple returns Expression:
@@ -110,8 +114,8 @@ ExpressionSimple returns Expression:
| {BlockExpr} "{" statements=Statements ("return" scope=Expression Sep*)? "}"
| {ListExpr} "[" indices=IndexExpr "]"
| InterpolatedString
| TupleExpr
| {TypeExpr} type=TupleTypeExpr
| "(" TupleExpr ")"
| {TypeExpr} SYM_LANGLE type=TupleTypeExpr SYM_RANGLE
| {SpecialVariable} "_"
;
Variable:
@@ -122,9 +126,9 @@ InterpolatedString:
| strings+=INTERPOL_START expressions+=Expression (strings+=INTERPOL_MIDDLE expressions+=Expression)* strings+=INTERPOL_END
;
TupleExpr returns Expression:
{TupleExpr} "(" ")"
| {TupleExpr} "(" positional+=Expression ("," positional+=Expression)* ("," names+=Name "=" named+=Expression)* (forceTuple?=",")? ")"
| {TupleExpr} "(" names+=Name "=" named+=Expression ("," names+=Name "=" named+=Expression)* (forceTuple?=",")? ")"
{TupleExpr}
| {TupleExpr} positional+=Expression ("," positional+=Expression)* ("," names+=Name "=" named+=Expression)* (forceTuple?=",")?
| {TupleExpr} names+=Name "=" named+=Expression ("," names+=Name "=" named+=Expression)* (forceTuple?=",")?
;
IndexExpr:
IndexExprSimple ( {ComprehensionExpr.creator=current} (steps+=ComprehensionStep)+
@@ -145,29 +149,30 @@ TypeModifiers returns TypeExpression:
TypeExtension
;
TypeExtension returns TypeExpression:
TypeConcretion ({TypeExtension.base=current} => "{" block=Statements "}")*
TypeConcretion
;
TypeConcretion returns TypeExpression:
TypeSimple ( {TypeValueConcretion.function=current} => argument=TupleExpr
| {TypeConcretion.base=current} => argument=TupleTypeExpr
| {TypeMemberAccessExpr.base=current} => "." memberName=Name)*
TypeSimple ( {TypeValueConcretion.function=current} => "(" argument=TupleExpr ")"
| {TypeConcretion.base=current} => SYM_LANGLE argument=TupleTypeExpr SYM_RANGLE
| {TypeMemberAccessExpr.base=current} => "." memberName=Name
| {TypeKindedExpr.base=current} => "::" kind=TypeSimple)*
;
TypeSimple returns TypeExpression:
{AbstractTypeExpr} "abstract"
| {TupleTypeExpr} "tuple"
{AbstractTypeExpr} "abstract" "{" block=Statements "}"
| {TupleTypeExpr} "tuple" "{" block=Statements "}"
| {TypeNumberLiteral} value=NUMBER
| {TypeStringLiteral} value=STRING
| {ValueType} value=TupleExpr
| {ValueType} "(" value=TupleExpr ")"
| TypeVariable
| TupleTypeExpr
| SYM_LANGLE TupleTypeExpr SYM_RANGLE
;
TypeVariable:
name=TypeName
;
TupleTypeExpr returns TypeExpression:
{TupleTypeExpr} "{" "}"
| {TupleTypeExpr} "{" => positional+=TypeExpression ("=" defaults+=Expression)? ("," positional+=TypeExpression ("=" defaults+=Expression)?)* ("," names+=Name ":" named+=TypeExpression ("=" defaults+=Expression)?)* (forceTuple?=",")? "}"
| {TupleTypeExpr} "{" names+=Name ":" named+=TypeExpression ("=" defaults+=Expression)? => ("," names+=Name ":" named+=TypeExpression ("=" defaults+=Expression)?)* (forceTuple?=",")? "}"
{TupleTypeExpr}
| {TupleTypeExpr} positional+=TypeExpression ("=" defaults+=Expression)? ("," positional+=TypeExpression ("=" defaults+=Expression)?)* ("," names+=Name ":" named+=TypeExpression ("=" defaults+=Expression)?)* (forceTuple?=",")?
| {TupleTypeExpr} names+=Name ":" named+=TypeExpression ("=" defaults+=Expression)? => ("," names+=Name ":" named+=TypeExpression ("=" defaults+=Expression)?)* (forceTuple?=",")?
;
terminal ID_LOWER:
@@ -178,7 +183,7 @@ terminal ID_BACKTICK:
terminal ID_UPPER:
('A'..'Z')('a'..'z'|'A'..'Z'|'_'|'0'..'9')*;
terminal ID_FORWARDTICK:
'' (!("\n"|''))* ''
'´' (!("\n"|'´'))* '´'
;
terminal fragment DEC:
('0'..'9')
@@ -0,0 +1,50 @@
/*
* generated by Xtext 2.12.0
*/
package org.bynar.versailles.xtext.formatting2
import com.google.inject.Inject
import org.bynar.versailles.xtext.services.VersaillesLangGrammarAccess
import org.bynar.versailles.xtext.versaillesLang.CompilationUnit
import org.bynar.versailles.xtext.versaillesLang.LetStmt
import org.eclipse.xtext.formatting2.AbstractFormatter2
import org.eclipse.xtext.formatting2.IFormattableDocument
import org.bynar.versailles.xtext.versaillesLang.DefStmt
import org.eclipse.emf.ecore.EObject
import org.eclipse.xtext.formatting2.internal.AbstractTextReplacer
import org.eclipse.xtext.formatting2.regionaccess.ITextSegment
import org.eclipse.xtext.formatting2.ITextReplacerContext
class SimpleTextReplacer extends AbstractTextReplacer {
String newText;
protected new(IFormattableDocument document, ITextSegment region, String newText) {
super(document, region)
this.newText = newText
}
override createReplacements(ITextReplacerContext context) {
context.addReplacement(region.replaceWith(newText))
context
}
}
class VersaillesLangFormatter extends AbstractFormatter2 {
@Inject extension VersaillesLangGrammarAccess
def dispatch void format(CompilationUnit compilationUnit, extension IFormattableDocument document) {
// TODO: format HiddenRegions around keywords, attributes, cross references, etc.
compilationUnit.allSemanticRegions.forEach[
if (it.text == "\\langle" || it.text == "<.")
document.addReplacer(new SimpleTextReplacer(document, it, ""))
else if (it.text == "\\rangle" || it.text == ".>")
document.addReplacer(new SimpleTextReplacer(document, it, ""))
else if (it.text == "->" || it.text == "\\leftarrow" || it.text == "\to")
document.addReplacer(new SimpleTextReplacer(document, it, ""))
]
}
}
@@ -30,7 +30,7 @@ Export-Package: org.bynar,
org.bynar.xtext.scoping,
org.bynar.xtext.serializer,
org.bynar.xtext.services,
org.bynar.xtext.validation
org.bynar.xtext.validation,
org.bynar.xtext.formatting2
Import-Package: org.apache.log4j
Bundle-ClassPath: lib/org.bynar.jar,
.
Bundle-ClassPath: lib/org.bynar.jar,.
@@ -31,15 +31,12 @@ TypeInt returns versailles::TypeExpression:
@Override
TypeSimple returns versailles::TypeExpression:
super
| BitTypeExpression
;
BitTypeExpression returns BitTypeExpression:
{BitFieldTypeExpr} "bits" (bitWidth=Expression)
| {ByteFieldTypeExpr} "bytes" (byteWidth=Expression)
| {BitFieldTypeExpr} "bits" "(" (bitWidth=Expression) ")"
| {ByteFieldTypeExpr} "bytes" "(" (byteWidth=Expression) ")"
| {RecordTypeExpr} "record" "{" statements=Statements "}"
| {UnionTypeExpr} "union" "{" statements=Statements "}"
| {RegisterTypeExpr} "register" bitWidth=Expression "{" statements=Statements "}"
| {ArrayTypeExpr} "array" "of" elementType=TypeExpression ("length" length=Expression | "until" until=Expression)
| {ArrayTypeExpr} "array" "of" elementType=TypeExpression ("length" "(" length=Expression ")" | "until" "(" until=Expression ")")
;
Interpretation:
@@ -10,7 +10,6 @@ import org.bynar.xtext.bynarLang.BitFieldTypeExpr
import org.bynar.xtext.bynarLang.ByteFieldTypeExpr
import org.bynar.xtext.bynarLang.RecordTypeExpr
import org.bynar.xtext.bynarLang.RecordComponent
import org.bynar.xtext.bynarLang.BitTypeExpression
import org.bynar.xtext.bynarLang.TypeWrittenExpr
import org.bynar.xtext.bynarLang.TypeConvertedExpr
import org.bynar.xtext.bynarLang.TypeWhereExpr
@@ -31,14 +31,22 @@ Workflow {
language = StandardLanguage {
name = "org.bynar.xtext.BynarLang"
fileExtensions = "bynar"
// parserGenerator = {
// options = {
// backtrack = true
// }
// }
serializer = {
generateStub = false
}
validator = {
// composedCheck = "org.eclipse.xtext.validation.NamesAreUniqueValidator"
}
referencedResource = "platform:/resource/org.bynar.versailles.xtext/model/generated/VersaillesLang.genmodel" }
}
formatter = {
generateStub = true
}
referencedResource = "platform:/resource/org.bynar.versailles.xtext/model/generated/VersaillesLang.genmodel"
}
}
}
@@ -0,0 +1,14 @@
/*
* generated by Xtext 2.12.0
*/
package org.bynar.xtext.formatting2
import com.google.inject.Inject
import org.bynar.versailles.xtext.formatting2.VersaillesLangFormatter
import org.bynar.xtext.bynarLang.RecordAlign
import org.bynar.xtext.bynarLang.RegisterComponent
import org.bynar.xtext.services.BynarLangGrammarAccess
import org.eclipse.xtext.formatting2.IFormattableDocument
class BynarLangFormatter extends VersaillesLangFormatter {
}
@@ -192,6 +192,7 @@ class DocBookGenerator(root1: Statement,
generateTypeDescription(t)
case t@Application(_, _) =>
<para>A { term2Xml(t) }.</para>
case _ => throw new Exception
}
}

0 comments on commit 7ff28c2

Please sign in to comment.