Skip to content

Commit

Permalink
improve support for constitutive terms in a party entitytype
Browse files Browse the repository at this point in the history
there is a code smell emanating from the page.
  • Loading branch information
mengwong committed Oct 20, 2021
1 parent b3b4b4f commit 4e80aaf
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 22 deletions.
53 changes: 33 additions & 20 deletions mengwong/mp/src/Lib.hs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ getStanzas esa = do
rs <- esa
let chunks = getChunks $ Location rs (0,0) ((0,0),(V.length (rs ! (V.length rs - 1)) - 1, V.length rs - 1))
toreturn = extractRange <$> glueChunks chunks
traceM ("getStanzas: extracted range " ++ (Text.unpack $ pShow toreturn))
-- traceM ("getStanzas: extracted range " ++ (Text.unpack $ pShow toreturn))
return toreturn

-- because sometimes a chunk followed by another chunk is really part of the same chunk.
Expand Down Expand Up @@ -181,7 +181,7 @@ getChunks loc@(Location rs (_cx,_cy) ((_lx,_ly),(_rx,ry))) =
all (\row -> V.all Text.null (rs ! row))
rows
]
toreturn = setRange loc <$> wantedChunks
toreturn = setRange loc <$> (filter (not . null) wantedChunks)
in -- trace ("getChunks: input = " ++ show [ 0 .. ry ])
-- trace ("getChunks: listChunks = " ++ show listChunks)
-- trace ("getChunks: wantedChunks = " ++ show wantedChunks)
Expand Down Expand Up @@ -209,7 +209,9 @@ setRange loc@(Location _rawStanza _c ((_lx,_ly),(_rx,_ry))) ys =
let cursorToEndLine = moveTo loc (0, last ys)
lineLen = lineLength cursorToEndLine - 1
cursorToEndRange = moveTo loc (lineLen, last ys)
in loc { cursor = (0,head ys)
in -- trace ("setRange: loc = " ++ show loc)
-- trace ("setRange: ys = " ++ show ys)
loc { cursor = (0,head ys)
, range = ((0,head ys),cursor cursorToEndRange) }

data Location = Location
Expand Down Expand Up @@ -326,18 +328,20 @@ pRule = withDepth 1 $ do

pConstitutiveRule :: Parser [Rule]
pConstitutiveRule = debugName "pConstitutiveRule" $ do
initialLocation <- lookAhead pXLocation
myTraceM $ "pConstitutiveRule: initial location = " ++ show initialLocation
term <- (pOtherVal <* dnl) <?> "defined term"

leftX <- lookAhead pXLocation -- this is the column where we expect IF/AND/OR etc.
leftY <- lookAhead pYLocation
(term,termalias) <- pTermParens
checkDepth

defWord <- pToken Means <|> pToken Is
leftX <- lookAhead pXLocation -- this is the column where we expect IF/AND/OR etc.
defWord <- pToken Means <|> pToken Is <|> pToken Includes
myTraceM $ "pConstitutiveRule: matched defWord " ++ show defWord
myTraceM $ "pConstitutiveRule: \"" ++ Text.unpack term ++ "\" " ++ show defWord ++ "..."
(ands,rs) <- withDepth leftX dBoolRules -- (foo AND (bar OR baz), [constitutive and regulative sub-rules])
return $ Constitutive term ands Nothing Nothing Nothing : rs

srcurl <- asks sourceURL
let srcref = SrcRef srcurl srcurl leftX leftY Nothing
let defalias = maybe [] (\t -> pure (DefTermAlias t term Nothing (Just srcref))) termalias

return $ Constitutive term ands Nothing Nothing Nothing : rs ++ defalias

pRegRule :: Parser [Rule]
pRegRule = debugName "pRegRule" $ (try pRegRuleSugary <|> pRegRuleNormal) <* optional dnl
Expand Down Expand Up @@ -384,9 +388,8 @@ pRegRuleSugary = debugName "pRegRuleSugary" $ do
pRegRuleNormal :: Parser [Rule]
pRegRuleNormal = debugName "pRegRuleNormal" $ do
leftX <- lookAhead pXLocation -- this is the column where we expect IF/AND/OR etc.
leftY <- lookAhead pYLocation
checkDepth
(_party_every, entitytype, entityalias) <- pActor Party <|> pActor Every
(_party_every, entitytype, _entityalias, defalias) <- pActor Party <|> pActor Every
-- (Who, (BoolStruct,[Rule]))
whoBool <- optional (withDepth leftX (preambleBoolRules [Who]))
-- the below are going to be permutables
Expand All @@ -397,9 +400,6 @@ pRegRuleNormal = debugName "pRegRuleNormal" $ do
myTraceM $ "pRegRuleNormal: permutations returned rulebody " ++ show rulebody
let (who, (cbs, brs)) = mergePBRS (if null (rbpbrs rulebody) then [(Always, (Nothing, []))] else rbpbrs rulebody)
let (ewho, (ebs, ebrs)) = fromMaybe (Always, (Nothing, [])) whoBool
srcurl <- asks sourceURL
let srcref = SrcRef srcurl srcurl leftX leftY Nothing
let defalias = maybe [] (\t -> pure (DefTermAlias t entitytype Nothing (Just srcref))) entityalias
let toreturn = Regulative
entitytype
(newPre (Text.pack $ show ewho) <$> ebs)
Expand Down Expand Up @@ -445,12 +445,25 @@ pTemporal = ( do

-- "PARTY Bob (the "Seller")
-- "EVERY Seller"
pActor :: MyToken -> Parser (MyToken, Text.Text, Maybe Text.Text)
pActor :: MyToken -> Parser (MyToken, Text.Text, Maybe Text.Text, [Rule])
pActor party = do
entitytype <- pToken party *> pOtherVal
entityalias <- optional pOtherVal
leftY <- lookAhead pYLocation
leftX <- lookAhead pXLocation -- this is the column where we expect IF/AND/OR etc.
-- add pConstitutiveRule here -- we could have "MEANS"
(entitytype, entityalias) <- lookAhead (pToken party *> pTermParens)
omgARule <- pConstitutiveRule <|> ([] <$ (pToken party *> pTermParens))
srcurl <- asks sourceURL
let srcref = SrcRef srcurl srcurl leftX leftY Nothing
let defalias = maybe [] (\t -> pure (DefTermAlias t entitytype Nothing (Just srcref))) entityalias
return (party, entitytype, entityalias, defalias ++ omgARule)

-- two tokens of the form | some thing | ("A Thing") | ; |
pTermParens :: Parser (Text.Text, Maybe Text.Text)
pTermParens = do
entitytype <- pOtherVal
entityalias <- optional pOtherVal -- TODO: add test here to see if the pOtherVal has the form ("xxx")
_ <- dnl
return (party, entitytype, entityalias)
return (entitytype, entityalias)

pDoAction :: Parser ActionType
pDoAction = pToken Do >> pAction
Expand Down
8 changes: 6 additions & 2 deletions mengwong/mp/test/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,10 @@ main = do
[ DefTermAlias "(\"singer\")" "person" Nothing
(Just (SrcRef {url = "test/Spec", short = "test/Spec", srcrow = 1, srccol = 1, version = Nothing})) ]

let if_king_wishes_singer_2 = if_king_wishes ++
[ DefTermAlias "(\"singer\")" "person" Nothing
(Just (SrcRef {url = "test/Spec", short = "test/Spec", srcrow = 1, srccol = 2, version = Nothing})) ]

it "should parse natural language aliases (\"NL Aliases\") aka inline defined terms" $ do
mycsv <- BS.readFile "test/nl-aliases.csv"
parseR (pRule <* eof) "" (exampleStream mycsv) `shouldParse` if_king_wishes_singer
Expand All @@ -272,8 +276,8 @@ main = do

it "should parse despite interrupting newlines" $ do
mycsv <- BS.readFile "test/blank-lines.csv"
parseR (pRule <* eof) "" (exampleStream mycsv) `shouldParse` if_king_wishes_singer

parseR (pRule <* eof) "" (head . tail $ exampleStreams mycsv) `shouldParse` if_king_wishes_singer_2
-- XXX: this is awful and needs to be fixed. wtf, head.tail?

-- upgrade single OR group to bypass the top level AND group

Expand Down

0 comments on commit 4e80aaf

Please sign in to comment.