How to handle EOF with the scan_fmt crate? - input

With the scan_fmt crate, how do I handle EOF, when used with the scanln_fmt helpers? I want to do something like this where x is None if a empty line was provided:
let (isEOF, x) = scanln_fmt_some!("{d}");
How can I distinguish between empty new line input and EOF?

The scan_fmt crate does not expose a way to distinguish between EOF and other errors.
The macros only return either a (Option<X>, Option<Y>, ...) (as from scan[ln]_fmt_some) or a Result<(X, Y, ...), ScanError> (as from scan[ln]_fmt). And ScanError's contents seems to only explain what type failed to parse, but not why.

Related

Are there line breaks in Elm?

I'm calling a function in Elm and some of the variable names I'm passing as parameters are kind of long, so I'd rather not put it all on 1 line. But I'm having trouble figuring out how to do that.
Normally, from experience with other languages, I'd imagine it might look something like this:
result =
Dict.foldr \
dataTransformationFunction \
0 \
initialStateDict
That obviously didn't work though. I tried using |> and <|, but I couldnt get anything using those to compile (since 0 and the other parameters arent functions that takes arguments, I couldn't pipe).
Am I doing something wrong here, or is there no way to add a line break in Elm code?
Looks like I forgot to try the simplest option; you don't need a special character/operator to do a line break if none of your parameters require any (more) arguments.
This is perfectly legal:
result =
Dict.foldr
(myFunction)
0
initialStateDict

Why no "each" method on Perl6 sequences?

Sometimes I'll start writing a chain of method calls at the Perl 6 REPL, like:
".".IO.dir.grep(...).map(...).
...and then I realize that what I want to do with the final list is print every element on its own line. I would expect sequences to have something like an each method so I could end the chain with .each(*.say), but there's no method like that that I can find. Instead I have to return to the beginning of the line and prepend .say for. It feels like it breaks up the flow of my thoughts.
It's a minor annoyance, but it strikes me as such a glaring omission that I wonder if I'm missing some easy alternative. The only ones I can think of are ยป.say and .join("\n").say, but the former can operate on the elements out of order (if I understand correctly) and the latter constructs a single string which could be problematically large, depending on the input list.
You can roll your own.
use MONKEY;
augment class Any
{
method each( &block )
{
for self -> $value {
&block( $value );
}
}
};
List.^compose;
Seq.^compose;
(1, 2).each({ .say });
(2, 3).map(* + 1).each({ .say });
# 1
# 2
# 3
# 4
If you like this, there's your First CPAN module opportunity right there.
As you wrote in the comment, just an other .map(*.say) does also create a line with True values when using REPL. You can try to call .sink method after the last map statement.
".".IO.dir.grep({$_.contains('e')}).map(*.uc).map(*.say).sink

Erlang - checking for unbound variables

Is there any way to know if a certain variable has been bound or not after an expression has been evaluated ?
My code:
{ok, After} = ts_dynvars:lookup(last, DynVars),
what I need to know if whether there is any "After" to work with or not. Perhaps that "ok" can be put to use for my purpose ?
Thanks!
If you (try to) refer to an unbound variable in an expression, it's a compilation error. In particular, after
{ok, After} = ts_dynvars:lookup(last, DynVars),
there are only two possibilities: if the pattern matches, After is bound, and can be used; if it doesn't, an exception will be thrown, and code which tries to work with After will never be executed.
UPDATE:
are you telling me there is no way to branch code execution in the situation in which the pattern does not match
Of course there is:
case ts_dynvars:lookup(last, DynVars) of
{ok, After} -> ...;
_ -> ... %% or other patterns
end
but the compiler won't let you use After in other branches or after case (unless all branches bind After).
is this exception not catchable at all
It is:
try
{ok, After} = ts_dynvars:lookup(last, DynVars),
...
catch
_:_ -> ...
end
but again, you won't be able to use After in catch sections or after try ends (you can bind a new variable named After, of course).
3 possibilities:
After is not yet bound and the ts_dynvars:lookup/2 returns a result of
the form {ok,Value} then After is bound to Value (a copy is made)
and the program goes to the next line.
After is already bound and ts_dynvars:lookup/2 returns exactly {ok,After}: the pattern matches;
After keeps its value; and the program goes to the next line.
ts_dynvars:lookup/2 returns Ret that does not match {ok,After} as
described in the 2 previous lines. Then the execution stops and the
VM throws the exception {badmatch,Ret}. If the line is in a catch or
a try section, the process will continue, otherwise it will crash. The next
line of code (if any) will never be executed.

Ignore MSGTokenError in JAVACC

I use JAVACC to parse some string defined by a bnf grammar with initial non-terminal G.
I would like to catch errors thrown by TokenMgrError.
In particular, I want to handle the following two cases:
If some prefix of the input satisfies G, but not all of the symbols are read from the input, consider this case as normal and return AST for found prefix by a call to G().
If the input has no prefix satisfying G, return null from G().
Currently I'm getting TokenMgrError 's in each of this case instead.
I started to modify the generated files (i.e, to change Error to Exception and add appropriate try/catch/throws statements), but I found it to be tedious. In addition, automatic generation of the modified files produced by JAVACC does not work. Is there a smarter way to accomplish this?
You can always eliminate all TokenMgrErrors by including
<*> TOKEN : { <UNEXPECTED: ~[] > }
as the final rule. This pushes all you issues to the grammar level where you can generally deal with them more easily.

ANTLR reports error and I think it should be able to resolve input with backtracking

I have a simple grammar that works for the most part, but at one place it reports error and I think it shouldn't, because it can be resolved using backtracking.
Here is the portion that is problematic.
command: object message_chain;
object: ID;
message_chain: unary_message_chain keyword_message?
| binary_message_chain keyword_message?
| keyword_message;
unary_message_chain: unary_message+;
binary_message_chain: binary_message+;
unary_message: ID;
binary_message: BINARY_OPERATOR object;
keyword_message: (ID ':' object)+;
This is simplified version, object is more complex (it can be result of other command, raw value and so on, but that part works fine). Problem is in message_chain, in first alternative. For input like obj unary1 unary2 it works fine, but for intput like obj unary1 unary2 keyword1:obj2 is trys to match keyword1 as unary message and fails when it reaches :. I would think that it this situation parser would backtrack and figure that there is : and recognize that that is keyword message.
If I make keyword message non-optional it works fine, but I need keyword message to be optional.
Parser finds keyword message if it is in second alternative (binary_message) and third alternative (just keyword_message). So something like this gives good results: 1 + 2 + 3 Keyword1:Value
What am I missing? Backtracking is set to true in options and it works fine in other cases in the same grammar.
Thanks.
This is not really a case for PEG-style backtracking, because upon failure that returns to decision points in uncompleted derivations only. For input obj unary1 unary2 keyword1:obj2, with a single token lookahead, keyword1 could be consumed by unary_message_chain. The failure may not occur before keyword_message, and next to be tried would be the second alternative of message_chain, i.e. binary_message_chain, thus missing the correct parse.
However as this grammar is LL(2), it should be possible to extend lookahead to avoid consuming keyword1 from within unary_message_chain. Have you tried explicitly setting k=2, without backtracking?