Alex lex.x compilation : Not in scope 'begin' - haskell

I have the below statements in Lex.x to parse block comments.
<0> "//".* { tokWValue LTokComment }
<0> "/*" { begin blockcomment }
<blockcomment> "*/" { begin 0 }
<blockcomment> . { tokWValue LTokComment }
But If I generate Lex.hs using Alex, it does not add the 'begin' function.
This results in the below compilation error.
src/Lex.x:367:18: Not in scope: ‘begin’
src/Lex.x:368:18: Not in scope: ‘begin’
Any idea what might be wrong?
I am using wrapper 'posn'

Start codes are only available when using any of the monad-... wrappers.
If you read the docs for the monad wrapper -- Section 5.3.3 - The "monad" wrapper -- you see that it is the first wrapper which keeps track of the start code.
You can also verify this by finding the alex wrapper files -- look for the directory containing the files AlexWrapper-basic, AlexWrapper-posn, etc. On OS X when installing the Haskell Platform they are located in a directory like /Library/Haskell/ghc-7.10.2-x86_64/share/alex-3.1.4. The functions begin and andBegin only occur in the monad-related wrappers.

Related

How to deal with crates exporting symbols with identical names

I'd like to use both just-argon2 and sodiumoxide which are bindings to Argon2 and libsodium respectively. The first I will use for password hashing, the second for encryption and more.
However, because libsodium itself also includes the Argon2 code, I get the following linker error when building:
error: linking with `link.exe` failed: exit code: 1169
|
= note: "C:\\Program Files (x86)\\Microsoft Visual Studio\\2019\\Preview\\VC\\Tools\\MSVC\\14.29.30132\\bin\\HostX64\\x64\\link.exe" "/NOLOGO" "/NXCOMPAT" "/LIBPATH:<omitted>\\rust-ms\\tools\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib" "<omitted>\\target\\debug\\deps\\sw_password_vault.sw_password_vault.cvsxie8m-cgu.0.rcgu.o" "<omitted>\\target\\debug\\deps\\sw_password_vault.sw_password_vault.cvsxie8m-cgu.1.rcgu.o" "<omitted>\\target\\debug\\deps\\sw_password_vault.sw_password_vault.cvsxie8m-cgu.10.rcgu.o" "<omitted>\\target\\debug\\deps\\sw_password_vault.sw_password_vault.cvsxie8m-cgu.11.rcgu.o" "<omitted>\\target\\debug\\deps\\sw_password_vault.sw_password_vault.cvsxie8m-cgu.12.rcgu.o" "<omitted>\\target\\debug\\deps\\sw_password_vault.sw_password_vault.cvsxie8m-cgu.13.rcgu.o" "<omitted>\\target\\debug\\deps\\sw_password_vault.sw_password_vault.cvsxie8m-cgu.14.rcgu.o" "<omitted>\\target\\debug\\deps\\sw_password_vault.sw_password_vault.cvsxie8m-cgu.15.rcgu.o" "<omitted>\\target\\debug\\deps\\sw_password_vault.sw_password_vault.cvsxie8m-cgu.2.rcgu.o" "<omitted>\\target\\debug\\deps\\sw_password_vault.sw_password_vault.cvsxie8m-cgu.3.rcgu.o" "<omitted>\\target\\debug\\deps\\sw_password_vault.sw_password_vault.cvsxie8m-cgu.4.rcgu.o" "<omitted>\\target\\debug\\deps\\sw_password_vault.sw_password_vault.cvsxie8m-cgu.5.rcgu.o" "<omitted>\\target\\debug\\deps\\sw_password_vault.sw_password_vault.cvsxie8m-cgu.6.rcgu.o" "<omitted>\\target\\debug\\deps\\sw_password_vault.sw_password_vault.cvsxie8m-cgu.7.rcgu.o" "<omitted>\\target\\debug\\deps\\sw_password_vault.sw_password_vault.cvsxie8m-cgu.8.rcgu.o" "<omitted>\\target\\debug\\deps\\sw_password_vault.sw_password_vault.cvsxie8m-cgu.9.rcgu.o" "/OUT:<omitted>\\target\\debug\\deps\\sw_password_vault.exe" "<omitted>\\target\\debug\\deps\\sw_password_vault.2501tt6qpnxmq6dq.rcgu.o" "/OPT:REF,NOICF" "/DEBUG" "/NATVIS:<omitted>\\rust-ms\\tools\\lib\\rustlib\\etc\\intrinsic.natvis" "/NATVIS:<omitted>\\rust-ms\\tools\\lib\\rustlib\\etc\\liballoc.natvis" "/NATVIS:<omitted>\\rust-ms\\tools\\lib\\rustlib\\etc\\libcore.natvis" "/NATVIS:<omitted>\\rust-ms\\tools\\lib\\rustlib\\etc\\libstd.natvis" "/LIBPATH:<omitted>\\target\\debug\\deps" "/LIBPATH:<omitted>\\target\\debug\\build\\just-argon2-fba8d38863f06c3e\\out" "/LIBPATH:C:\\Users\\Steven\\.cargo\\registry\\src\\github.com-1ecc6299db9ec823\\libsodium-sys-0.2.7\\msvc/x64/Debug/v142/" "/LIBPATH:<omitted>\\rust-ms\\tools\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib" "<omitted>\\target\\debug\\deps\\libargon2-47416e342f12fc80.rlib" "<omitted>\\target\\debug\\deps\\libbitflags-033856bffcb41e1a.rlib" "<omitted>\\target\\debug\\deps\\libsodiumoxide-ae3f70995957a7e5.rlib" "<omitted>\\target\\debug\\deps\\libserde-02c238cdfdb411bb.rlib" "<omitted>\\target\\debug\\deps\\libed25519-a72dcd735d2405a2.rlib" "<omitted>\\target\\debug\\deps\\libsignature-cb8ca284112f3bdb.rlib" "<omitted>\\target\\debug\\deps\\liblibsodium_sys-90db65a5d41df800.rlib" "<omitted>\\target\\debug\\deps\\liblibc-db8e81727092f722.rlib" "<omitted>\\rust-ms\\tools\\lib\\rustlib\\x86_64-pc-windows-msvc\\lib\\libstd-7d8f1d3120dc2b31.rlib" "<omitted>\\lib\\libpanic_unwind-00b4871c13b6f72d.rlib" "<omitted>\\lib\\libstd_detect-38300272c9518b1b.rlib" "<omitted>\\lib\\librustc_demangle-008ea074760d3d54.rlib" "<omitted>\\lib\\libhashbrown-5d102da67e495133.rlib" "<omitted>\\lib\\librustc_std_workspace_alloc-108ed3dcf248b873.rlib" "<omitted>\\lib\\libunwind-bc9246c93f49e49a.rlib" "<omitted>\\lib\\libcfg_if-7ca234fdcd728c26.rlib" "<omitted>\\lib\\liblibc-e328514fb4ed0383.rlib" "<omitted>\\lib\\liballoc-5d0a4c6a1ffa6373.rlib" "<omitted>\\lib\\librustc_std_workspace_core-d379e9227cab087f.rlib" "<omitted>\\lib\\libcore-825774e96423c2c5.rlib" "<omitted>\\lib\\libcompiler_builtins-d5d1908505fa83bc.rlib" "advapi32.lib" "ws2_32.lib" "userenv.lib" "msvcrt.lib"
= note: liblibsodium_sys-90db65a5d41df800.rlib(blake2b-ref.obj) : error LNK2005: blake2b_init already defined in libargon2-47416e342f12fc80.rlib(blake2b.o)
liblibsodium_sys-90db65a5d41df800.rlib(blake2b-ref.obj) : error LNK2005: blake2b_init_key already defined in libargon2-47416e342f12fc80.rlib(blake2b.o)
liblibsodium_sys-90db65a5d41df800.rlib(blake2b-ref.obj) : error LNK2005: blake2b_init_param already defined in libargon2-47416e342f12fc80.rlib(blake2b.o)
liblibsodium_sys-90db65a5d41df800.rlib(blake2b-ref.obj) : error LNK2005: blake2b_update already defined in libargon2-47416e342f12fc80.rlib(blake2b.o)
liblibsodium_sys-90db65a5d41df800.rlib(blake2b-ref.obj) : error LNK2005: blake2b_final already defined in libargon2-47416e342f12fc80.rlib(blake2b.o)
liblibsodium_sys-90db65a5d41df800.rlib(blake2b-ref.obj) : error LNK2005: blake2b already defined in libargon2-47416e342f12fc80.rlib(blake2b.o)
Creating library C:\dev\Rust\SWPasswordVault\target\debug\deps\sw_password_vault.lib and object C:\dev\Rust\SWPasswordVault\target\debug\deps\sw_password_vault.exp
LINK : warning LNK4098: defaultlib 'LIBCMTD' conflicts with use of other libs; use /NODEFAULTLIB:library
C:\dev\Rust\SWPasswordVault\target\debug\deps\sw_password_vault.exe : fatal error LNK1169: one or more multiply defined symbols found
Both libraries export the same blake2b_* symbols, which are used by the Argon2 implementations, so the names clash.
Now you may be wondering: why not just use the libsodium pwhash API, which uses Argon2 internally (hence the linker error)? Because I want to use parallel Argon2, which is not exposed by libsodium, as it hardcodes the degree of parallelism to 1.
Apparently, the blake2b_* symbols are marked as ARGON2_LOCAL, so they are not exported, but I guess that this does not matter since they still exist for the linker.
One solution could be to use a pure Rust implementation of Argon2 such as rust-argon2, but I tested that one and unfortunately it is a lot slower.
How can I still use both libraries together? Do I have to make a separate library crate using just just-argon2 and export the functions provided by the just-argon2 crate or something like that, or would that not work / is there a better way? EDIT: this does not work.
Minimum non-compiling program:
use sodiumoxide;
use argon2;
fn main() { sodiumoxide::init(); }
While composing this program I noticed that when I swap the use statements it compiles fine, but it crashes at runtime:
use argon2;
use sodiumoxide;
fn main() {
sodiumoxide::init().expect("Could not init libsodium");
let password = "pass";
let salt = sodiumoxide::crypto::pwhash::gen_salt().0;
let mut key_bytes = [0u8; 32];
// INSECURE cost parameters
argon2::id_hash_raw(1, 1 << 10 /*1 MiB*/, 1,
Some(password.as_bytes()), Some(&salt), &mut key_bytes)
.unwrap_or_else(|err| panic!("Error hashing: {:?}", err));
}
This crashes with a STATUS_STACK_BUFFER_OVERRUN Windows exception (__fastfail(FAST_FAIL_STACK_COOKIE_CHECK_FAILURE)), while it works fine when not using any sodiumoxide functions (and an all-zero salt, for example). Apparently the different exports clash or something?
By the way: I'm very new to Rust so it's safe to say I don't know all the ins and outs of it.

Puzzling "info" message regarding package body requirement using Ada?

I am experiencing a peculiar "info" message from GNAT 7.4.0 (running on an "Ubuntu 19.04" system) while in the early stages of developing a QR-code generator.
I'm using some fairly aggressive compilation switches:
gnatmake -gnata -gnateE -gnateF -gnatf -gnato -gnatv -gnatVa -gnaty -gnatwe -gnatw.e main.adb
My code does build without errors, but this info message does suggest that I'm not providing a body for the package "qr_symbol".
qr_symbol.ads
with QR_Versions; use QR_Versions;
generic
Ver : QR_Version;
package QR_Symbol is
procedure Export_As_SVG;
private
type Module_State is (
Uncommitted,
One,
Zero
);
type Module_Family is (
Uncommitted,
Finder,
Separator,
Alignment,
Timing,
Format_Spec,
Version_Spec,
Data_Codeword,
EC_Codeword,
Padding
);
type Module is
record
State : Module_State := Uncommitted;
Family : Module_Family := Uncommitted;
end record;
type Module_Matrix is array (
Positive range <>,
Positive range <>
) of Module;
end QR_Symbol;
qr_symbol.adb
with Ada.Text_IO; use Ada.Text_IO;
package body QR_Symbol is
Version : constant QR_Version := Ver; -- Ver is a formal generic parameter
Side_Length : constant Positive := 17 + (Positive (Ver) * 4);
Matrix : Module_Matrix (1 .. Side_Length, 1 .. Side_Length);
procedure Export_As_SVG is
begin
Put_Line ("in Export_As_SVG()...");
Put_Line (" Version: " & Version'Image);
Put_Line (" Side_Length: " & Side_Length'Image);
-- Matrix (1, 1).State := One;
Put_Line (" Matrix (1, 1).State: " & Matrix (1, 1).State'Image);
end Export_As_SVG;
end QR_Symbol;
And here's the info output that I do not understand...
GNAT 7.4.0
Copyright 1992-2017, Free Software Foundation, Inc.
Compiling: qr_symbol.adb
Source file time stamp: 2019-12-07 16:29:37
Compiled at: 2019-12-07 16:29:38
==============Error messages for source file: qr_symbol.ads
9. procedure Export_As_SVG;
|
>>> info: "QR_Symbol" requires body ("Export_As_SVG" requires completion)
29 lines: No errors, 1 info message
aarch64-linux-gnu-gnatbind-7 -x main.ali
aarch64-linux-gnu-gnatlink-7 main.ali
Program output (given correct input, does gives correct output)...
$ ./main '' V1
QR Version requested: V 1
in Export_As_SVG()...
Version: 1
Side_Length: 21
Matrix (1, 1).State: UNCOMMITTED
QUESTION:
Why is there an info message suggesting that I need to provide a body for this package when it is clear that I have already done so?
An info message is not used to suggest that you should change your program, only to provide some (useful or not) information. In your case, the information is true. If it weren't fulfilled, it'd turn to an error.
You may want to check if this flag is causing the generation of this message:
According to GNAT User's Guide:
-gnatw.e
`Activate every optional warning.'
This switch activates all optional warnings, including those which are not activated by -gnatwa. The use of this switch is not
recommended for normal use. If you turn this switch on, it is almost
certain that you will get large numbers of useless warnings. The
warnings that are excluded from -gnatwa are typically highly
specialized warnings that are suitable for use only in code that has
been specifically designed according to specialized coding rules.
And if you don't want to remove that switch, at least you can disable this specific info message:
-gnatw.Y
`Disable information messages for why package spec needs body.'
This switch suppresses the output of information messages showing why a package specification needs a body.

RBParser message nodes and targeting the receiver and the argument?

Trying to get some of my old code up and running in Pharo. Some method names are different but after some hardship I managed to find equivalents that work.
I am parsing my code and I'd like to check if the receiver or any of the arguments is aSymbol in an effort to match them to supported alternatives. I've managed to do this to selectors, by analysing RBMessageNode s
aNode selector == aSymbol ifTrue: [ aNode selector: replacementSymbol ].
How can this be done to arguments and receivers? Is there a comprehensive guide on RBParser somewhere?
By direct manipulation
Assuming that you are looking for cases like this:
aSymbol message: aSymbol message: aSymbol
For receiver you should do:
(aNode isMessage and: [
aNode receiver isVariable and: [
aNode receiver name = 'aSymbol' ]]) ifTrue: [
"do your job here" ]
Here is another example on how to replace #aSymbol arguments with #newSymbol:
messageNode arguments: (messageNode arguments collect: [ :arg |
(arg isLiteralNode and: [ arg value = #aSymbol ])
ifFalse: [ arg ]
ifTrue: [ | newNode |
newNode := #aNewSymbol asLiteralNode.
arg replaceSourceWith: newNode.
newNode ] ]).
methodClass compile: ast newSource
The replaceSourceWith: makes sure that just a source will be replaced, but for newSource to actually return a new source you also need to swap the nodes themselves, that's why I'm doing a collect on arguments and return the new ones where needed.
You can view help about RBParser in Word Menu > Help > Help Browser > Refactoring Framework.
You can also play around by inspecting
RBParser parseExpression: 'aSymbol message: aSymbol message: aSymbol'
and looking at its contents
By Parse Tree Transformation
You can use pattern code to match and replace certain code. For example to change the symbol argument of a perform: message you can do this:
ast := yourMethod parseTree.
rewriter := RBParseTreeRewriter new
replace: '`receiver perform: #aSymbol'
with: '`receiver perform: #newSelector'.
(rewriter executeTree: ast) ifTrue: [
yourMethod class compile: ast newSource ]
You can learn more about the pattern matching syntax in the help topic Word Menu > Help > Help Browser > Refactoring Framework > Refactoring Engine > RBPatternParser …. I thing that MatchTool from pharo catalog can greatly help you in testing the match expressions (it also has a dedicated help topic about the matching syntax) while RewriteTool can help you to preview how your code will be transformed.

AC_ARG_ENABLE in an m4_foreach_w loop: no help string

I wish to generate a lot of --enable-*/--disable-* options by something like:
COMPONENTS([a b c], [yes])
where the second argument is the default value of the automatic enable_* variable. My first attempt was to write an AC_ARG_ENABLE(...) within an m4_foreach_w, but so far, I'm only getting the first component to appear in the ./configure --help output.
If I add hand-written AC_ARG_ENABLEs, they work as usual.
Regardless, the --enable-*/--disable-* options work as they should, just the help text is missing.
Here's the full code to reproduce the problem:
AC_INIT([foo], 1.0)
AM_INIT_AUTOMAKE([foreign])
AC_DEFUN([COMPONENTS],
[
m4_foreach_w([component], [$1], [
AS_ECHO(["Processing [component] component with default enable=$2"])
AC_ARG_ENABLE([component],
[AS_HELP_STRING([--enable-[]component], [component] component)],
,
[enable_[]AS_TR_SH([component])=$2]
)
])
AC_ARG_ENABLE([x],
[AS_HELP_STRING([--enable-[]x], [component x])],
,
[enable_[]AS_TR_SH([x])=$2]
)
AC_ARG_ENABLE([y],
[AS_HELP_STRING([--enable-[]y], [component y])],
,
[enable_[]AS_TR_SH([y])=$2]
)
])
COMPONENTS([a b c], [yes])
for var in a b c x y; do
echo -n "\$enable_$var="
eval echo "\$enable_$var"
done
AC_CONFIG_FILES(Makefile)
AC_OUTPUT
And an empty Makefile.am. To verify that the options work:
$ ./configure --disable-a --disable-b --disable-d --disable-x
configure: WARNING: unrecognized options: --disable-d
...
Processing component a with default enable=yes
Processing component b with default enable=yes
Processing component c with default enable=yes
$enable_a=no
$enable_b=no
$enable_c=yes
$enable_x=no
$enable_y=yes
After I poked around in autoconf sources, I figured out this has to do with the m4_divert_once call in the implementation of AC_ARG_ENABLE:
# AC_ARG_ENABLE(FEATURE, HELP-STRING, [ACTION-IF-TRUE], [ACTION-IF-FALSE])
# ------------------------------------------------------------------------
AC_DEFUN([AC_ARG_ENABLE],
[AC_PROVIDE_IFELSE([AC_PRESERVE_HELP_ORDER],
[],
[m4_divert_once([HELP_ENABLE], [[
Optional Features:
--disable-option-checking ignore unrecognized --enable/--with options
--disable-FEATURE do not include FEATURE (same as --enable-FEATURE=no)
--enable-FEATURE[=ARG] include FEATURE [ARG=yes]]])])dnl
m4_divert_once([HELP_ENABLE], [$2])dnl
_AC_ENABLE_IF([enable], [$1], [$3], [$4])dnl
])# AC_ARG_ENABLE
# m4_divert_once(DIVERSION-NAME, CONTENT)
# ---------------------------------------
# Output CONTENT into DIVERSION-NAME once, if not already there.
# An end of line is appended for free to CONTENT.
m4_define([m4_divert_once],
[m4_expand_once([m4_divert_text([$1], [$2])])])
I'm guessing that the HELP-STRING argument is remembered in it's unexpanded form, so it is added just once for all components. Manually expanding the AC_HELP_STRING does what I want:
AC_DEFUN([COMPONENTS],
[
m4_foreach_w([comp], [$1], [
AS_ECHO(["Processing component 'comp' with default enable=$2"])
AC_ARG_ENABLE([comp],
m4_expand([AS_HELP_STRING([--enable-comp], enable component comp)]),
,
[enable_[]AS_TR_SH([comp])=$2]
)
])
])
COMPONENTS([a b c x y], [yes])
I couldn't find a way to properly quote components so that it appears as a string, after being used as the loop variable in m4_foreach_w, so I just renamed it to spare me the trouble.

Perl Module Error - defined(%hash) is deprecated

Background:
I am working to migrate a Linux server to a newer one from Ubuntu 10.04 to 12.04
This server is responsible for executing several a number of Perl modules via crontabs.
These Perl Modules rely heavily on 30-40 perl extensions.
I have installed all Perl extensions and the crontabs are able to process successfully except for several Syntax errors caused by the newer versions of these Perl extensions.
I need some help with modifying the syntax to get the Perl script to process as intended.
Errors:
defined(%hash) is deprecated at pm/Alerts/Alerts.pm line 943.
(Maybe you should just omit the defined()?)
defined(%hash) is deprecated at pm/Alerts/Alerts.pm line 944.
(Maybe you should just omit the defined()?)
Code:
###
# Iterate the arrays deleting identical counts from each.
# If we found a mismatch then die.
# If either array is not empty when we are done then die
$logger->info('Comparing ' . (scalar keys %cms_rows) . ' CMS symbols to ' . (scalar keys %stats_rows) . ' STATS symbols');
foreach my $symbol ( keys %cms_rows ) {
my %cms_row = delete $cms_rows{$symbol};
my %stats_row = delete $stats_rows{$symbol};
##LINE 943## die("Error: NULL CMS counts for symbol '$symbol'") unless defined %cms_row;
##LINE 944## die("Error: NULL Stats counts for symbol '$symbol'") unless defined %stats_row;
my $cms_json = encode_json(\%cms_row);
my $stats_json = encode_json(\%stats_row);
$logger->debug("Comparing counts for '$symbol': CMS($cms_json), Stats($stats_json)");
die("Error: Up Counts Don't match for symbol '$symbol': CMS($cms_json), Stats($stats_json)") unless (!defined $cms_row{1} && !defined $stats_row{1}) || $cms_row{1} == $stats_row{1};
die("Error: Down Counts Don't match for symbol '$symbol': CMS($cms_json), Stats($stats_json)") unless (!defined $cms_row{-1} && !defined $stats_row{-1}) || $cms_row{-1} == $stats_row{-1};
}
###
Hopefully someone can help with this, any help is appreciated.
You must have upgraded from a seriously old version of Perl. The Perl 5.6.1 release notes say:
defined(%hash) is deprecated
(D) defined() is not usually useful on hashes because it checks for an
undefined scalar value. If you want to see if the hash is empty, just
use if (%hash) { # not empty } for example.
It was always a pretty stupid thing to do and Perl now warns you that you're doing something stupid. The warning is pretty clear about what you should do to fix this:
Maybe you should just omit the defined()?
So your lines would become:
die("Error: NULL CMS counts for symbol '$symbol'") unless %cms_row;
die("Error: NULL Stats counts for symbol '$symbol'") unless %stats_row;

Resources