Crystal parser creates an ASTNode that should be Crystal::Expressions at runtime but is somehow a Crystal::Nop - abstract-syntax-tree

require "compiler/crystal/syntax"
s = "a = 5; puts a + 3"
nodes = Crystal::Parser.parse(s)
puts nodes.class # => Crystal::Expressions
puts nodes.is_a? Crystal::Expressions # => true
puts nodes.is_a? Crystal::Nop # => false
puts nodes.expressions
So, I would assume the last expression to give an Array (or an ArrayLiteral node). However, I get
undefined method 'expressions' for Crystal::Nop (compile-time type is Crystal::ASTNode+)
Which makes no sense. .class and .is_a? are runtime checks, so the nodes , which has a compile-time type of ASTNode should be Crystal::Expressions, not Crystal::Nop.
The behaviour is the same on crystal versions 0.25.0, 0.25.1, 0.26.0, 0.26.1 and the version currently on the master branch of the git repo.

The error is raised at compile time because not all subclasses of Crystal::ASTNode have #expressions method. Crystal::Nop just happens to be the first such subclass the compiler checks and subsequently decides to throw an error. The way to fix the code is:
require "compiler/crystal/syntax"
s = "a = 5; puts a + 3"
nodes = Crystal::Parser.parse(s)
case nodes
when Crystal::Expressions
puts nodes.expressions
when Crystal::Nop
puts "It's a nop"
else
puts "Unhandles case for #{nodes.class}"
end
Alternatively, you can force the compiler to assume it's Crystal::Expressions by using .as:
nodes.as(Crystal::Expressions).expressions
Credit for this answer goes to straight-shoota on this Github issue

Related

Rails 6 warning: Overwriting existing method <model_name>.fetched_state

Rails 6.0.2.1
ruby 2.6.5p114 (2019-10-01 revision 67812) [x86_64-linux]
my ArticleDir class has 2 scopes:
scope :active, -> { where(active: true).where(expired_on: nil) }
scope :fetched_state, -> { where(state: ArticleDir::FETCHED.to_s) }
and a function:
def article_engine_counts(keyword_reln = Keyword.active_keywords)
joins(:keywords, :article_engine)
.where(Keyword.contains(keyword_reln))
.where(self.table[:active].eq(true))
.group(:state, ArticleEngine.table[:name]).count
end
On running the function in rails console, I get:
irb(main):108:0> ArticleDir.article_engine_counts(keyword)
Creating scope :active. Overwriting existing method ArticleDir.active.
Creating scope :fetched_state. Overwriting existing method ArticleDir.fetched_state.
(1.7ms) SELECT COUNT(*) AS count_all,
article_commons.state AS article_commons_state, sengines.name
AS sengines_name FROM article_commons INNER JOIN
directory_keywords ON directory_keywords.article_dir_id =
article_commons.id INNER JOIN keywords ON keywords.id =
directory_keywords.keyword_id INNER JOIN sengines ON
sengines.id = article_commons.sengine_id AND sengines.type
= 'ArticleEngine' WHERE article_commons.type = 'ArticleDir' AND keywords.id IN (1217) AND article_commons.active = TRUE GROUP
BY article_commons.state, sengines.name
=> {["expired", "data..."]=>1, ["fetched", "data..."]=>83, ["sourced", " data..."]=>81}
I've seen one other reference to this issue:
https://github.com/rails/rails/issues/31234
where it was suggested that the message related to overwriting a Kernel method.
I've checked the Kernel and no such methods exist on the Kernel in the first place to overwrite:
irb(main):002:0> Kernel.methods.grep(/active/)
=> []
irb(main):004:0> Kernel.methods.grep(/fetched_state/)
=> []
I am assuming that the message means what it seems to imply - arel / rails is somehow overwriting those two scopes on the model.
If so, why? and what do I do about it?
This can happen when you have something such as an enum that add scopes to the model.
If, in your model, you had an enum with :active as one of the values, e.g.:
enum status: [ :active, :archived ]
to go along with your explicit scope, you would see this warning.

.Net Core 3 Preview SequenceReader Length Delimited Parsing

I'm trying to use SequenceReader<T> in .Net Core Preview 8 to parse Guacamole Protocol network traffic.
The traffic might look as follows:
5.error,14.some text here,1.0;
This is a single error instruction. There are 3 fields:
OpCode = error
Reason = some text here
Status = 0 (see Status Codes)
The fields are comma delimited (semi-colon terminated), but they also have the length prefixed on each field. I presume that's so that you could parse something like:
5.error,24.some, text, with, commas,1.0;
To produce Reason = some, text, with, commas.
Simple comma delimited parsing is simple enough to do (with or without SequenceReader). However, to utilise the length I've tried the following:
public static bool TryGetNextElement(this ref SerializationContext context, out ReadOnlySequence<byte> element)
{
element = default;
var start = context.Reader.Position;
if (!context.Reader.TryReadTo(out ReadOnlySequence<byte> lengthSlice, Utf8Bytes.Period, advancePastDelimiter: true))
return false;
if (!lengthSlice.TryGetInt(out var length))
return false;
context.Reader.Advance(length);
element = context.Reader.Sequence.Slice(start, context.Reader.Position);
return true;
}
Based on my understanding of the initial proposal, this should work, though also could be simplified I think because some of the methods in the proposal make life a bit easier than that which is available in .Net Core Preview 8.
However, the problem with this code is that the SequenceReader does not seem to Advance as I would expect. It's Position and Consumed properties remain unchanged when advancing, so the element I slice at the end is always an empty sequence.
What do I need to do in order to parse this protocol correctly?
I'm guessing that .Reader here is a property; this is important because SequenceReader<T> is a mutable struct, but every time you access .SomeProperty you are working with an isolated copy of the reader. It is fine to hide it behind a property, but you'd need to make sure you work with a local and then push back when complete, i.e.
var reader = context.Reader;
var start = reader.Position;
if (!reader.TryReadTo(out ReadOnlySequence<byte> lengthSlice,
Utf8Bytes.Period, advancePastDelimiter: true))
return false;
if (!lengthSlice.TryGetInt(out var length))
return false;
reader.Advance(length);
element = reader.Sequence.Slice(start, reader.Position);
context.Reader = reader; // update position
return true;
Note that a nice feature of this is that in the failure cases (return false), you won't have changed the state yet, because you've only been mutating your local standalone clone.
You could also consider a ref-return property for .Reader.

rkafka.read() doesn't return a message (Returns double quotes only)

Trying to return a message through rkafka library in R.
Followed the same rkafka documentation # https://cran.r-project.org/web/packages/rkafka/vignettes/rkafka.pdf
Output returns "" without the actual message in it. Kafka tool confirms that the message is sent by the producer.
CODE:
prod1=rkafka.createProducer("127.0.0.1:9092")
rkafka.send(prod1,"test","127.0.0.1:9092","Testing once")
rkafka.closeProducer(prod1)
consumer1=rkafka.createConsumer("127.0.0.1:2181","test")
print(rkafka.read(consumer1))
Output:
[1] ""
Desired Output would return "Testing once".
In order to read the messages of a topic that have already been written to the topic (before the consumer has been started) you need to set offset value to the smallest possible (equivalent to --from-beginning). According to rkafka docs autoOffseetReset argument defaults to largest
autoOffsetReset
smallest : automatically reset the offset to the
smallest offset largest : automatically reset the offset to the
largest offset anything else: throw exception to the consumer
Required:Optional Type:String default:largest
In order to be able to consume messages you need to set autoOffsetReset to "smallest".
consumer1=rkafka.createConsumer("127.0.0.1:2181","test", autoOffsetReset="smallest")
Update: This Code Works:
library(rkafka)
prod1=rkafka.createProducer("127.0.0.1:9092")
rkafka.send(prod1,"test","127.0.0.1:9092","Testing once")
rkafka.send(prod1,"test","127.0.0.1:9092","Testing twice")
rkafka.closeProducer(prod1)
consumer1=rkafka.createConsumer("127.0.0.1:2181","test",groupId = "test-consumer-
group",zookeeperConnectionTimeoutMs = "100000",autoCommitEnable = "NULL",
autoCommitInterval = "NULL",autoOffsetReset = "NULL")
print(rkafka.read(consumer1))
print(rkafka.readPoll(consumer1))
rkafka.closeConsumer(consumer1)
The key is to restart Kafka after deleting the logs it generates.

How does the assignment part work in the following line of code in Angular2?

I am learning from the project angular2-rxjs-chat application ong github. In the code here there is a line of code given below:
threads[message.thread.id] = threads[message.thread.id] ||
message.thread;
where threads has earlier been defined on line 29 in the code as shown below:
let threads: {[key: string]: Thread} = {};
The comments in the code states that "store the message's thread in our acuuculator 'threads'. I need a little bit explanation of how does the assignment works on line 31 as on both sides of the assignment operator we have the same thing i.e., threads[message.thread.id]. If the statement on line 31 was like
(threads[message.thread.id] = message.thread;)
then I would explain it as a value is being assigned to a key in the map "threads". But I don't understand the full line.
This means if threads[message.thread.id] already has a value then keep it, otherwise set the value to meassage.thread.
If the part before || evaluates to a value that is truthy (not null, undefined, false, ...)then the part after||is not evaluated and the result from the part before||is returned otherwise the result from the expression after||` is returned.
You could also write it as
if(!threads[message.thread.id]) {
threads[message.thread.id] = message.thread;
}

I am having trouble adding elements to a referenced vector of a map

EDIT: Forgot to mention that my programming language is C++, and my IDE is Qt Creator.
I don't understand why I am having trouble adding elements to a referenced vector of a map (i.e. sometimes it works and sometimes it fails).
For example:
class C_html4_tags {
public:
C_html4_tags();
//.......
typedef std::vector<S_html_attr_value> TYPE_attr_values;
//.....
};
//...
C_html4_tags::C_html4_tags() {
//........
map<S_browser, TYPE_attr_values> attr_supported_attr_values_map;
S_browser dummy_browser; //a fake browser key for referencing a fully Html 4.01-compliant supported attr values list
dummy_browser.name = "Dummy";
//.....
TYPE_attr_values& attr_supported_attr_values = attr_supported_attr_values_map[dummy_browser];
//......
**attr_supported_attr_values = attr_supported_attr_values_map[dummy_browser];**
//...
**attr_supported_attr_values.clear();
attr_supported_attr_values_map.clear();**
//......
**attr_supported_attr_values = attr_supported_attr_values_map[dummy_browser];**
//...
**attr_supported_attr_values.clear();
attr_supported_attr_values_map.clear();**
}
I do the bolded lines multiple times with a bunch of different attributes, with very little difference between them, without a problem until reaching this one attribute (a_tabindex_attr), in which the IDE doesn't report anything wrong when running it normally (except "The program has unexpectedly finished." However, when debugging it, it now reports:
Signal received
The inferior stopped because it received a signal from the Operating
System.
Signal name : SIGSEGV Signal meaning : Segmentation fault
And the backtrace points to the attribute I mentioned above, on the line of code which does this:
attr_supported_attr_values.clear();
Now, after adding some debugging cout lines to the code, I learned that what's happening is, for some reason, even after doing:
attr_supported_attr_values.push_back(attr_value);
the vector object which is the returned mapped value of:
attr_supported_attr_values = attr_supported_attr_values_map[dummy_browser];
is not actually being changed when I push_back an S_html_attr_value object to the referenced vector. Now, I don't why that is, since I do pretty much the exact same thing in a bunch of other attributes' code before this one, without any problem, but for some reason now, on this one, it ain't adding the object to attr_supported_attr_values (which is a reference to the returned mapped value of 'dummy_browser'). And I know this for a fact, because I outputted the size of the internal mapped value object of the map (using an iterator for the map), and it was 0 after the call to push_back(). However, what is even more odd is, I ALSO outputted attr_supported_attr_values.size() after the call to push_back(), and that was 1! Now how could that be since they're both supposed to be the same object??
Note:
The full code of what I'm talking about is below (or at least the attribute code, minus the debugging statements...):
//a_tabindex_attr:
attr_desc = "Specifies the position of an <a> element in the\n"
"tabbing order for the current document.\n"
"The tabbing order defines the order in which\n"
"elements will receive focus when navigated by\n"
"the user via the keyboard. The tabbing order\n"
"may include elements nested within other elements.\n"
"Elements that may receive focus based on tabindex\n"
"adhere to the following rules:\n\n"
"1. Those elements that support this attribute and\n"
"assign a positive value to it are navigated first.\n"
"Navigation proceeds from the element with the\n"
"lowest tabindex value to the element with the\n"
"highest value. Values need not be sequential\n"
"nor must they begin with any particular value.\n"
"Elements that have identical tabindex values\n"
"should be navigated in the order they appear\n"
"in the character stream.\n"
"2. Those elements that do not support this\n"
"attribute or support it and assign it a value\n"
"of \"0\" are navigated next. These elements are\n"
"navigated in the order they appear in the\n"
"character stream.\n"
"3. Elements that are disabled do not participate\n"
"in the tabbing order.";
attr_supported_attr_values = attr_supported_attr_values_map[dummy_browser];
attr_value_desc = "A number you specify for the tab index/order.\n"
"It must be a value between 0 and 32767.";
attr_value_content = "<i>number</i>";
attr_value = C_html4_attributes_obj.getAttrValue(attr_value_desc, attr_value_content,
attr_value_supported_browsers,
attr_value_required, attr_value_deprecated,
attr_value_notes, attr_value_tips);
attr_value.is_relative = true;
attr_supported_attr_values.push_back(attr_value);
try {
N_init_class_members::initAttr(C_html4_attributes::attr_tabindex, a_tabindex_attr, attr_desc,
attr_supported_browsers, attr_supported_attr_values_map, attr_required,
attr_deprecated, attr_notes, attr_tips);
}
catch (const char* exc) {
string exc_str = "Error! Call to N_init_class_members::initAttr() from\n"
"constructor of C_html4_tags class. That function\n"
"reported the following exception:\n\n";
exc_str += exc;
throw (exc_str.c_str()); //re-throw exception
}
a_tabindex_attr.is_standard_attr = true;
a_tabindex_attr.is_optional_attr = true;
//cleanup from attribute operations so we can begin working on the next attribute:
C_html4_attributes_obj.clear(); //wipes the slate clean for all the supported attributes' properties except content
attr_desc.clear();
attr_supported_attr_values.clear();
attr_supported_attr_values_map.clear();
attr_value_desc.clear();
attr_value_content.clear();

Resources