Here is my code: http://pastie.org/private/t37ectjdnplit66zidj6hq
This code is for a pascal-like language parser. I have a problem in these lines : 178 , 181, 193. When I have a defined grammar with the keyword : [Any], I run it successfully ! but when I replace that keyword by one of traits and "case classes" , it notifies me have a mistake ! I think I don't understand the meanings when use the code : parser[???]. Can anybody help me to resolve these problems?
line 181: def val_type : Parser[Type] = primitive| array_type | string_type
line 193: def ident_list : Parser[List[Id]] = ident ~(rep(","~> ident))
Updated:
Here are the corresponding errors:
line 181: type mismatch; found : MPRecognizer.this.Parser[Any] required: MPRecognizer.this.Parser[Type]
line 193: type mismatch; found : MPRecognizer.this.Parser[MPRecognizer.this.~[String,List[String]]] required: MPRecognizer.this.Parser[Id]
You need to show us the original code, not the one you modified by making everything return Parser[Any].
The error messages tell you that not all alternatives produce an instance of Type. For example, check that the methods primitive, array_type and string_type all return a Parser[Type]. Add the type annotation to each of them, and if you get type errors in the modified definitions, narrow it down further in the same way.
Related
This has been puzzling me for a while... I have done research, tried lots of things but failed miserably. The time has come to ask here.
My grammar has this rule to define types:
MyTypeDeclaration returns XExpression:
=>({MyTypeDeclaration} type=JvmTypeReference name=ValidID '(')
(params+=FullJvmFormalParameter (',' params+=FullJvmFormalParameter)*)?
')' block=XBlockExpression
;
Of course, in the inferrer, I map it to a class (with a supertype MySupertype to differentiate from other Java classes)
members += f.toClass(f.fullyQualifiedName) [
superTypes += typeRef(MySupertype)
...
members += f.toConstructor [
for (p : f.params)
parameters += p.toParameter(p.name, p.parameterType)
body = f.block
]
...
]
What I need is to invoke this class as a function, e.g. using XFeatureCall. XFeatureCall::feature is a JvmIdentifiableElement and so is MyTypeDeclaration when mapped (in the compiler I will add a "new" prefix to call the class constructor). However, naturally, XFeatureClass does not include Java classes in its default scope.
So the question is, how to change this behavior? I need to include MyTypeDeclaration instances (or, more generally, Java classes with MySupertype as superclass) in XFeatureClass scope. I looked at the type computer, getLinkingCandidates and al but it looks too arcane for me.
I use version 2.15 as I need GWT...
Please help as I am really stuck at this point...
Thanks,
Martin
I was trying to compile libfriends (source) against valac (.28) and libgee (1.0). I specifically compiled these against Ubuntu-16.04 stack.
But I am getting following error
entry.vala:397.38-397.38: warning: if-statement without body
if (_selected != value);
^
entry.vala:172.52-172.86: error: Argument 1: Cannot convert from `GLib.TypeClass' to `GLib.ObjectClass'
binding_set = Gtk.BindingSet.by_class (typeof (InputTextView).class_ref ());
I don't really find anything wrong with code. Any Idea?
The entire buildlog is here: https://launchpadlibrarian.net/263631082/buildlog_ubuntu-xenial-i386.libfriends_0.1.2+14.10.20140709+201606051415~ubuntu16.04.1_BUILDING.txt.gz
I just checked and it compiles with valac-0.18, but doesn't compile with valac-0.28.
So there must have been a change between those valac versions that does more strict type checking in this case.
GLib.TypeClass (really GTypeClass in C) is the parent class of GLib.ObjectClass (really GObjectClass in C).
So the compiler is correct to not allow this without a cast. I don't know if the cast is correct in this situation, but it makes the code compile:
binding_set = Gtk.BindingSet.by_class ((ObjectClass) typeof (InputTextView).class_ref ())
See also valadoc for GObjectClass where a similar typecast is done in the example code:
http://valadoc.org/#!api=gobject-2.0/GLib.ObjectClass
Good day,
I have problem. I want to simulate some errors in hacklang.
<?hh
namespace Exsys\HHVM;
class HHVMFacade{
private $vector = Vector {1,2,3};
public function echoProduct() : Vector<string>{
return $this->vector;
}
public function test(Vector<string> $vector) : void{
var_dump($vector);
}
}
Function echoProduct() returns Vector of strings. But private property $vector is Vector of integers. When I call echoFunction and returning value use as argument for function test(). I get
object(HH\Vector)#35357 (3) { [0]=> int(1) [1]=> int(2) [2]=> int(3) }
Why? I am expecting some error because types mismatch.
There's two things at play here:
Generics aren't reified, so the runtime has no information about them. This means the runtime is only checking that you're returning a Vector.
$this->vector itself isn't typed. This means the type checker (hh_client) treats it as a unknown type. Unknown types match against everything, so there's no problem returning an unknown type where a Vector<string> is expected.
This is to allow you to gradually type your code. Whenever a type isn't known, the type checker just assumes that the developer knows what's happening.
The first thing I'd do is change the file from partial mode to strict mode, which simply involves changing from <?hh to <?hh // strict. This causes the type checker to complain about any missing type information (as well as a couple of other things, like no superglobals and you can't call non-Hack code).
This produces the error:
test.hh:6:13,19: Please add a type hint (Naming[2001])
If you then type $vector as Vector<int> (private Vector<int> $vector), hh_client then produces:
test.hh:9:16,28: Invalid return type (Typing[4110])
test.hh:8:44,49: This is a string
test.hh:6:20,22: It is incompatible with an int
test.hh:8:44,49: Considering that this type argument is invariant with respect to Vector
Which is the error you expected. You can also get this error simply by adding the type to $vector, without switching to strict mode, though I prefer to write my Hack in the strongest mode that the code supports.
With more recent versions of HHVM, the type checker is called whenever Hack code is run (there's an INI flag to turn this off), so causing the type mismatch will also cause execution of the code to fail.
I'd like to run through a simple Rascal MPL parsing example, and am trying to follow Listing 1 from the Rascal Language Workbench (18531D.pdf) of May 3rd 2011. I've downloaded the current Rascal MPL version 0.5.1, and notice that a few module paths have changed. The following shows the content of my Entities.rsc:
module tut1::Entities
extend lang::std::Layout;
extend lang::std::Id;
extend Type;
start syntax Entities
= entities: Entity* entities;
syntax Entity
= #Foldable entity: "entity" Id name "{" Field* "}";
syntax Field
= field: Symbol Id name;
I'm assuming here that what was Name and Ident is now Id; and what was Type is now Symbol. I then continue as follows:
rascal>import tut1::Entities;
ok
rascal>import ParseTree;
ok
However, when I attempt to execute the crucial parse function, I receive the errors listed below. Where am I going wrong? (Despite the message I note that I can declare a Symbol variable at the Rascal prompt.)
rascal>parse(#Entities, "entity Person { string name integer age }");
Extending again?? ParseTree
Extending again?? Type
expanding parameterized symbols
generating stubs for regular
generating literals
establishing production set
generating item allocations
computing priority and associativity filter
printing the source code of the parser class
|prompt:///|(22,43,<1,22>,<1,65>): Java("Undeclared non-terminal: Symbol, in class: class org.rascalmpl.java.parser.object.$shell$")
org.rascalmpl.parser.gtd.SGTDBF.invokeExpects(SGTDBF.java:139)
org.rascalmpl.parser.gtd.SGTDBF.expandStack(SGTDBF.java:864)
org.rascalmpl.parser.gtd.SGTDBF.expand(SGTDBF.java:971)
org.rascalmpl.parser.gtd.SGTDBF.parse(SGTDBF.java:1032)
org.rascalmpl.parser.gtd.SGTDBF.parse(SGTDBF.java:1089)
org.rascalmpl.parser.gtd.SGTDBF.parse(SGTDBF.java:1082)
org.rascalmpl.interpreter.Evaluator.parseObject(Evaluator.java:493)
org.rascalmpl.interpreter.Evaluator.parseObject(Evaluator.java:544)
org.rascalmpl.library.Prelude.parse(Prelude.java:1644)
org.rascalmpl.library.Prelude.parse(Prelude.java:1637)
sun.reflect.NativeMethodAccessorImpl.invoke0(NativeMethodAccessorImpl.java:-2)
somewhere in: $shell$
The example is out-of-date. Something like this would work better:
module tut1::Entities
extend lang::std::Layout; // for spaces and such
extend lang::std::Id; // for the Id non-terminal
start syntax Entities
= entities: Entity* entities;
syntax Entity
= #Foldable entity: "entity" Id name "{" Field* "}";
syntax Field
= field: Id symbol Id name; // now Id is used instead of Symbol and "symbol" is just the name of a slot in the rule
Some explanation:
the imports/extends have gone, they were unnecessary and might be confusing
there was a missing definition for the Symbol non-terminal. I don't know what it was supposed to do, but it should have been defined syntax Symbol = ..., but it did not make sense to me and instead I reused Id to define the type of a field.
the type checker (under development) would have warned you before using the parse function.
I'm getting started with Irony (version Irony_2012_03_15) but I pretty quickly got stuck when trying to generate an AST. Below is a completely strpped language that throws the exception:
[Language("myLang", "0.1", "Bla Bla")]
public class MyLang: Grammar {
public NModel()
: base(false) {
var number = TerminalFactory.CreateCSharpNumber("number");
var binExpr = new NonTerminal("binExpr", typeof(BinaryOperationNode));
var binOp = new NonTerminal("BinOp");
binExpr.Rule = number + binOp + number;
binOp.Rule = ToTerm("+");
RegisterOperators(1, "+");
//MarkTransient(binOp);
this.Root = binExpr;
this.LanguageFlags = Parsing.LanguageFlags.CreateAst; // if I uncomment this line it throws the error
}
}
As soon as I uncomment the last line it throws a NullReferenceException in the grammar explorer or when i want to parse a test. The error is on AstBuilder.cs line 96:
parseNode.AstNode = config.DefaultNodeCreator();
DefaultNodeCreator is a delegate that has not been set.
I've tried setting things with MarkTransient etc but no dice.
Can someone help me afloat here? I'm proably missing something obvious. Looked for AST tutorials all over the webs but I can't seem to find an explanation on how that works.
Thanks in advance,
Gert-Jan
Once you set the LanguageFlags.CreateAst flag on the grammar, you must provide additional information about how to create the AST.
You're supposed to be able to set AstContext.Default*Type for the whole language, but that is currently bugged.
Set TermFlags.NoAstNode. Irony will ignore this node and its children.
Set AstConfig.NodeCreator. This is a delegate that can do the right thing.
Set AstConfig.NodeType to the type of the AstNode. This type should be accessible, implement IAstInit, and have a public, no-parameters constructor. Accessible in this case means either public or internal with the InternalsVisibleTo attribute.
To be honest, I was facing the same problem and did not understand Jay Bazuzi answer, though it looks like valid one(maybe it's outdated).
If there's anyone like me;
I just inherited my Grammar from Irony.Interpreter.InterpretedLanguageGrammar class, and it works. Also, anyone trying to get AST working, make sure your nodes are "public" :- )
On top of Jay's and Erti-Chris's responses, this thread is also useful:
https://irony.codeplex.com/discussions/361018
The creator of Irony points out the relevant configuration code in InterpretedLanguageGrammar.BuildAst.
HTH