How parse Perl 6 code using STD.pm6 grammar? - parsing

I would like minimal working example of parsing Perl 6 code from Perl 6 using STD.pm6
Running something like this
use v6;
use STD.pm6;
my $j = slurp 'e:\src\perl6\valid-p6-script.p6';
my $o = STD::Grammar.parse($j);
say $o.perl;
under debugger give me absolutely weird output:
e:\src\prg\perl6>c:\rakudo\bin\perl6-debug-m.bat -I. p6.p6
>>> LOADING p6.p6
>>> LOADING E:\src\prg\perl6\STD.pm6
Unhandled exception: ctxlexpad needs an MVMContext
at gen/moar/m-CORE.setting:15136 (C:\rakudo/share/perl6/runtime/CORE.setting.moarvm::24)
from gen/moar/m-CORE.setting:15250 (C:\rakudo/share/perl6/runtime/CORE.setting.moarvm:AT-KEY:95)
from gen/moar/m-CORE.setting:4291 (C:\rakudo/share/perl6/runtime/CORE.setting.moarvm:postcircumfix:<{ }>:4294967295)
from C:\rakudo/share/perl6/lib/Debugger/UI/CommandLine.pm:736 (C:\rakudo\share\perl6\lib\Debugger\UI\CommandLine.pm.mo
arvm::82)
from gen/moar/m-CORE.setting:2826 (C:\rakudo/share/perl6/runtime/CORE.setting.moarvm:sink-all:168)
from gen/moar/m-CORE.setting:10187 (C:\rakudo/share/perl6/runtime/CORE.setting.moarvm:sink:36)
from C:\rakudo/share/perl6/lib/Debugger/UI/CommandLine.pm:730 (C:\rakudo\share\perl6\lib\Debugger\UI\CommandLine.pm.mo
arvm:unhandled:114)
from C:\rakudo/share/perl6/lib/Debugger/UI/CommandLine.pm:721 (C:\rakudo\share\perl6\lib\Debugger\UI\CommandLine.pm.mo
arvm::25)
from gen/moar/m-Metamodel.nqp:3586 (C:\rakudo\share\nqp\lib/Perl6/Metamodel.moarvm:enter:28)
from gen/moar/m-CORE.setting:5190 (C:\rakudo/share/perl6/runtime/CORE.setting.moarvm:CALL-ME:57)
from gen/moar/m-BOOTSTRAP.nqp:2749 (C:\rakudo/share/nqp/lib/Perl6/BOOTSTRAP.moarvm::93)
from gen\moar\stage2\NQPHLL.nqp:1425 (C:\rakudo\share\nqp\lib/NQPHLL.moarvm:command_eval:380)
from src/Perl6/Compiler.nqp:59 (C:\rakudo\share\nqp\lib/Perl6/Compiler.moarvm:command_eval:93)
from gen\moar\stage2\NQPHLL.nqp:1365 (C:\rakudo\share\nqp\lib/NQPHLL.moarvm:command_line:114)
from gen/moar/m-perl6-debug.nqp:497 (C:\rakudo\share\perl6\runtime\perl6-debug.moarvm:MAIN:190)
from gen/moar/m-perl6-debug.nqp:440 (C:\rakudo\share\perl6\runtime\perl6-debug.moarvm:<mainline>:68)
from <unknown>:1 (C:\rakudo\share\perl6\runtime\perl6-debug.moarvm:<main>:8)
from <unknown>:1 (C:\rakudo\share\perl6\runtime\perl6-debug.moarvm:<entry>:9)
or without debugger:
e:\src\prg\perl6>c:\rakudo\bin\perl6.bat -I. p6.p6
===SORRY!=== Error while compiling E:\src\prg\perl6\STD.pm6
Semicolon form of 'grammar' without 'unit' is illegal. You probably want to use 'unit grammar'
at E:\src\prg\perl6\STD.pm6:8
------> :ver<6.0.0.alpha>:auth<http://perl.org>;<HERE><EOL>
from p6.p6:2

STD.pm6 was the reference grammar/verifier maintained by Larry during the development of the various Perl 6 compilers (Rakudo, Pugs, Niecza, ...). It comes with a dedicated tool called viv that translates it to Perl 5. As far as I know, it never ran on any of the mainstream compilers.
With the 6.c language release, the torch of reference implementation has essentially passed on to Rakudo, which uses its own grammar written in NQP to parse Perl 6.
While this Grammar (in combination with the corresponding actions) can be used for manual parsing by importing these modules :from<NQP>, the more friendly approach (eg no need to declare $*LINEPOSCACHE) would be via the corresponding compiler object:
say nqp::getcomp('perl6').eval($source, :target<parse>).dump;
Other targets of interest might be :target<syntaxcheck> as well as the more low-level :target<ast> or for the curious perhaps even :target<mast>.

Working example from #perl6 conversation:
use nqp;
use Perl6::Grammar:from<NQP>;
use Perl6::Actions:from<NQP>;
my $source = q|say "hello world";|;
my $*LINEPOSCACHE;
my $p6c = Perl6::Grammar.parse($source, :actions(Perl6::Actions.new()));
say $p6c.dump

Related

Haskell-src-exts throws TemplateHaskell error

I'm trying to use the haskell-src-exts package to parse Haskell modules. Currently, I'm trying to parse the acme-io package's module, but I keep getting this error no matter what parse mode I try:
*** Exception: fromParseResult: Parse failed at [System/IO/Unsafe/Really/IMeanIt] (1:57): TemplateHaskell is not enabled
The module mentioned makes no references to TemplateHaskell, not in it's LANGUAGE pragma, nor is there a $ anywhere in the source file.
I'm wondering if my parse mode has something to do with it - here it is:
defaultParseMode { parseFilename = toFilePath m
, baseLanguage = Haskell2010
, extensions = []
, ignoreLanguagePragmas = True
, ignoreLinePragmas = True
, fixities = Nothing
}
I've also tried to replace the extensions field with knownExtensions from the parsing suite, without any luck.
This is a duplicate question of this answer - using the parseFile function fixed the issue. However, the reader should note that haskell-src-exts uses different parsing than GHC - I ran into another similar issue right after this, because haskell-src-exts can't handle multi-param contexts without -XMultiParamTypeClasses, yet GHC can, borking the parser if you're scraping Hackage. Hint may be a better option, can't say for sure though.

Compile with standalone flag gives compilation errors in client code

I'm attempting to compile Zero29 with the --standalone compiler flag. The project itself compiles fine, but I have a unit test project that exercises some code in the Zero29 project, even though it's an executable program (.exe).
Everything works fine without the --standalone compilation flag.
However, when I add the --standalone compilation flag to the Zero29 project, the Zero29 project compiles fine, but in the unit test project, the compiler complains about this Discriminated Union defined in the Zero29 project:
namespace Ploeh.ZeroToNine
open System
open Ploeh.ZeroToNine.Versioning
type Arg =
| Assign of Version
| AssignRank of Rank * int
| Increment of Rank
| ListVersions
| ShowHelp
| Unknown of string list
The unit test project directly references the Zero29 project:
Zero29.UnitTests --references--> Zero29 (where --standalone is added)
When I attempt to compile the entire solution, the Zero29 project compiles with the --standalone flag, but then compilation of Zero29.UnitTests fails. There are several errors, but they are all the same, so here's a single example:
error FS0039: The value or constructor 'Assign' is not defined
Which points to the third line of this code:
let ParseAssignVersionReturnsCorrectResult(version : string) =
let actual = [| "-a"; version |] |> Args.Parse
verify <# [Assign(Version version)] = (actual |> Seq.toList) #>
The strange thing is that while the compiler complains about Assign in the third line of this code snippet, it doesn't complain about the use of Args.Parse, even though it's defined in the same code file as the Arg Discriminated Union.
Why does it do that, and how can I resolve this issue?
(I've attempted to distil the problem here, but the links I've provided point to the actual code files on GitHub, if more information is required.)
Libraries compiled with the --standalone switch cannot expose any F# datatypes. This is, for one, expressly stated in Pickering (2007), p. 210. In your case, a discriminated union is one of these prohibited types. The fact that the file is an executable changes nothing here: it becomes a library the moment you attempt to use it as one.
There have been also multiple reports (for example, here and here) that even libraries compiled with --standalone behave, quoting one of these sources, “funky.” It would be safe to say that the use of this switch should perhaps be limited to stand-alone executables only (and they cannot pretend to be a library even when under unit tests).
Pickering R. (2007). Foundations of F#. Apress.

Paamayim nekudotayims in PHP 5.2

I can upgrade php 5.2 in my server. I have to make this server work today (the vacation I have planned tomorrow is under question because of this error) with new testlink. I am stuck with following error i.e Paamayim nekudotayims.
What changes I should do to resolve it?
This link contains the file with the bug.
The Scope Resolution Operator (also called Paamayim Nekudotayim) or in simpler terms, the double colon, is a token that allows access to static, constant, and overridden properties or methods of a class.
SO may be in your codes you try to call static method or properties with wrong operator.
From Wikipedia:
In PHP, the scope resolution operator is also called Paamayim
Nekudotayim (Hebrew: פעמיים נקודתיים‎), which means “double colon” in
Hebrew.
The name "Paamayim Nekudotayim" was introduced in the
Israeli-developed Zend Engine 0.5 used in PHP 3. Although it has been
confusing to many developers who do not speak Hebrew, it is still
being used in PHP 5, as in this sample error message:
$ php -r :: Parse error: syntax error, unexpected
T_PAAMAYIM_NEKUDOTAYIM
As of PHP 5.4, error messages concerning the scope resolution operator
still include this name, but have clarified its meaning somewhat:
$ php -r :: Parse error: syntax error, unexpected '::'
(T_PAAMAYIM_NEKUDOTAYIM)

XML parsing in Ruby

I am using a REXML Ruby parser to parse an XML file. But on a 64 bit AIX box with 64 bit Ruby, I am getting the following error:
REXML::ParseException: #<REXML::ParseException: #<RegexpError: Stack overflow in
regexp matcher:
/^<((?>(?:[\w:][\-\w\d.]*:)?[\w:][\-\w\d.]*))\s*((?>\s+(?:[\w:][\-\w\d.]*:)?[\w:][\-\w\d.]*\s*=\s*(["']).*?\3)*)\s*(\/)?>/mu>
The call for the same is something like this:
REXML::Document.new(File.open(actual_file_name, "r"))
Does anyone have an idea regarding how to solve this issue?
I've had several issues for REXML, it doesn't seem to be the most mature library. Usually I use Nokogiri for Ruby XML parsing stuff, it should be faster and more stable than REXML. After installing it with sudo gem install nokogiri, you can use something like this to get a DOM instance:
doc = Nokogiri.XML(File.open(actual_file_name, 'rb'))
# => #<Nokogiri::XML::Document:0xf1de34 name="document" [...] >
The documentation on the official webpage is also much better than that of REXML, IMHO.
I almost immediately found the answer.
The first thing I did was to search in the ruby source code for the error being thrown.
I found that regex.h was responsible for this.
In regex.h, the code flow is something like this:
/* Maximum number of duplicates an interval can allow. */
#ifndef RE_DUP_MAX
#define RE_DUP_MAX ((1 << 15) - 1)
#endif
Now the problem here is RE_DUP_MAX. On AIX box, the same constant has been defined somewhere in /usr/include.
I searched for it and found in
/usr/include/NLregexp.h
/usr/include/sys/limits.h
/usr/include/unistd.h
I am not sure which of the three is being used(most probably NLregexp.h).
In these headers, the value of RE_DUP_MAX has been set to 255! So there is a cap placed on the number of repetitions of a regex!
In short, the reason is the compilation taking the system defined value than that we define in regex.h!
This also answers my question which i had asked recently:
Regex limit in ruby 64 bit aix compilation
I was not able to answer it immediately as i need to have min of 100 reputation :D :D
Cheers!

problem antlrworks code too large

In Antlrworks I get this error:
[18:21:03] Checking Grammar Grammar.g...
[18:21:26] Grammar.java:12: code too large
[18:21:26] public static final String[] tokenNames = new String[] {
[18:21:26] ^
[18:21:26] 1 error
Using instead the generated code in a Java project works normally. What can be had this problem?
Thanks.
For larger grammars, it's easier to split your grammar into bite-sized chunks (at least a separate lexer and parser). If you do so, ANTLRWorks will probably stop complaining as well.
Checkout the Wiki entry about "Composite grammars".

Resources