Skip to content

Make peg-grammar available in all threads for peg/compile#1703

Merged
bakpakin merged 1 commit intojanet-lang:masterfrom
amano-kenji:peg
Feb 1, 2026
Merged

Make peg-grammar available in all threads for peg/compile#1703
bakpakin merged 1 commit intojanet-lang:masterfrom
amano-kenji:peg

Conversation

@amano-kenji
Copy link
Contributor

@amano-kenji amano-kenji commented Jan 24, 2026

It fixes #1180.

Although I don't know how to write C, who knew the pull request is going to be easy for a person who knows almost nothing about C? Today, I'm your code monkey who types shakespeare. The code monkey God gave me an insight for this pull request.

I tested it with

(ev/do-thread
  (pp (peg/match '{:main ':s*} " ")))

which prints

@[" "]

Your code monkey also found that sogaiu replaced default-peg-grammar with (dyn :peg-grammar). This change broke peg/compile.

@sogaiu
Copy link
Contributor

sogaiu commented Jan 24, 2026

found that sogaiu replaced default-peg-grammar with (dyn :peg-grammar). This change broke peg/compile.

Perhaps you wouldn't mind indicating the specifics?

@amano-kenji
Copy link
Contributor Author

Ouch. I discovered that bakpakin replaced default-peg-grammar with (dyn :peg-grammar) in 85155bb.

I was careless in monkey mode.

@sogaiu
Copy link
Contributor

sogaiu commented Jan 24, 2026

Thanks for the details.

Errors happen. No harm done.

@sogaiu
Copy link
Contributor

sogaiu commented Jan 24, 2026

FWIW, according to the commit message for 85155bb, the change in question was allegedly part of activity supporting this endeavor.

Note that this change has been in place for just about 5 years (from 2021-01-24).

@amano-kenji
Copy link
Contributor Author

amano-kenji commented Jan 24, 2026

Perhaps, default-peg-grammar should become immutable? You can change (dyn :peg-grammar) even if default-peg-grammar is immutable.

@amano-kenji
Copy link
Contributor Author

amano-kenji commented Jan 24, 2026

@bakpakin Should default-peg-grammar become a struct instead of a table? #478 can be an outdated optimization at this point.

We can avoid loading the full core environment if the mutable parts of core are not reachable from the main function.

It seems to me that you don't really need to modify default-peg-grammar during runtime.... In practice, people just want the default. If people wanted more shared grammar, then they can carry it outside default-peg-grammar. This is also certainly more functional and referentially transparent. I decrease complexity by sticking to referential transparency unless I have to deviate from it.

Or, perhaps this pull request is good enough because most small scripts don't launch a new thread....

@bakpakin
Copy link
Member

bakpakin commented Feb 1, 2026

There was some concern originally about accidentally pulling in the entire core library just to compile PEGs, but I think that is not a realistic problem for most users - the current architecture was originally done to decouple Pegs from core. There are plenty of ways to optimize this but I think this is the simplest solution for now.

@bakpakin bakpakin merged commit a95546f into janet-lang:master Feb 1, 2026
13 checks passed
@sogaiu
Copy link
Contributor

sogaiu commented Feb 1, 2026

In practice, people just want the default. If people wanted more shared grammar, then they can carry it outside default-peg-grammar.

FWIW, I've added to default-peg-grammar before. That lets you provide things to more than one grammar by only making changes in one place.

@bakpakin
Copy link
Member

bakpakin commented Feb 1, 2026

Hmm, I just realized some other issues with this change, perhaps I merged too soon. Let me test some stuff locally, but I think may want to revert this. Sorry @amano-kenji

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

peg-grammar is not available in threads

3 participants